grunt.template - acecode/grunt GitHub Wiki

模板字符串可以被模板函数自动被解析,此外(众多任务使用的)config.get方法自动展开Gruntfile中配置的类似<% %>的配置项

grunt.template.process

处理Lo-Dash template字符串,template参数会被迭代展开直到不存在未处理的模板标记。

默认的数据对象是整个配置对象,可以传入自定义的options.data来覆盖;默认的模板分隔符是<% %>,可以使用options.delimiters来覆盖

grunt.template.process(template [, options])

在模板内部,grunt对象是默认可用的以支持类似 <%= grunt.template.today('yyyy') %>的用法。注意一旦数据对象有自己的grunt参数,gruntAPI就被覆盖掉,在模板中不可用了

在这个例子中,baz属性将一直被展开,直到不含未处理的<% %>模板为止

var obj = {
  foo: 'c',
  bar: 'b<%= foo %>d',
  baz: 'a<%= bar %>e'
};
grunt.template.process('<%= baz %>', {data: obj}) // 'abcde'

grunt.template.setDelimiters

设置Lo-Dash template 使用的一组分隔符以便手动调用grunt.util._.template,默认包括预设的分隔符<% %>.

可能你不会用到这个方法, 因为你可以直接加参数使用grunt.template.process,它会在内部调用这个方法

grunt.template.setDelimiters(name)

grunt.template.addDelimiters

增加一组命名的Lo-Dash template 分隔符. 可能你不会用到这个方法, 因为默认的就足够好用了, 但是你还是可以加入 {% %}[% %] 这样的分隔符.

grunt.template.addDelimiters(name, opener, closer)

Helpers

grunt.template.date

dateformat library 来格式化日期.

grunt.template.date(date, format)

这里日期被格式化成月/日/年

grunt.template.date(847602000000, 'yyyy-mm-dd') // '1996-11-10'

grunt.template.today

dateformat library 来格式化当前.

grunt.template.today(format)

这里转换当前日期为四位数年份

grunt.template.today('yyyy') // '2013'

(有人提醒我每年更新这个年份,这样文档才能显得与时俱进)

⚠️ **GitHub.com Fallback** ⚠️