我正在编写一个 grunt 插件,它必须处理任务中定义的模板。我遇到的问题是,即使在 grunt.template.process() 之前,模板中的变量也被删除了。
该任务的选项如下所示:
wrap: {
html: {
footer: '<script>require(["<%= filename %>"])</script>',
src: ['templates/*.*'],
dest: 'new_templates'
}
}
任务本身的简化形式如下所示:
grunt.registerMultiTask('wrap', 'Blah-blah', function() {
grunt.log.writeln(this.data.footer); // outputs "<script>require([""]); </script>"
});
最佳答案
Grunt 自动使用 <% %>
扩展模板在调用任务之前在其配置中添加分隔符,请参阅 http://gruntjs.com/configuring-tasks#templates .
因此,如果您想按原样获取模板字符串,一种解决方案是在配置字符串中使用自定义分隔符(例如 [%
或 {%
),然后在 grunt.template.process
中使用自定义分隔符。调用(请参阅 http://gruntjs.com/api/grunt.template#grunt.template.process 和 http://gruntjs.com/api/grunt.template#grunt.template.adddelimiters )
关于javascript - grunt.registerMultiTask() 从字符串中删除变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31741813/