grunt.template
可以手工模板处理模版字符串。另外,con.get (被很多任务所使用)会解析 <% %>类型的模版字符串,此类型的模版字符串是在 Gruntfile 中指的配置数据中定义的。
处理 字符串。template 参数将被递归处理,知道没有任何模版位置。
认的数据对象是整个配置对象,但是,如果设置了options.data,则使用该对象。认的模板分隔符是<% %>,但是,如果options.delimiters被设置为的形式(通过grunt.template.addDelimiters 进行设置),那么后续就会使用此模板分隔符。
grunt.template.process(template [, options])
在模板内部暴露了grunt对象,因此你可以这样做<%= grunt.template.tody('yyyy') %>。注意, 如果数据对象已经有了grunt,那么在模板内部将无法访问grunt API。
在下面这个案例中,baz 被递归处理,直到再也没有多余的 <% %> 模版需要处理。
var obj = { foo: 'c', bar: 'b<%= foo %>d', baz: 'a<%= bar %>e' }; grunt.template.process('<%= baz %>', {data: obj}) // 'abcde'
设置的分隔符为预定义的形式,以防需要手动grunt.util._.template。con分隔符认是<% %>。
你可能永远不会使用这个,因为你所使用的 grunt.template.process 在内部使用的就是这个。
grunt.template.setDelimiters(name)
为命名分隔符。你或许不需要使用这个,因为内置的分割符应该足以满足需求了,但是你仍可以随时{% %} 或者[% %]风格的分隔符。
name 参数应当是唯一的,因为这也是我们通过 grunt.template.setDelimiters 分隔符的方式,并且后续作为 grunt.template.process 的选项来使用。
grunt.template.addDelimiters(name, opener, closer)
在此实例中,如果我们希望使用 {% %} 形式的分隔符,我们需要如下这样设置:
grunt.template.addDelimiters('myDelimiters', '{%', '%}')
使用格式化日期。
grunt.template.date(date, format)
个案例中,指定的日期被格式化为 month/day/year。
grunt.template.date(847602000000, 'yyyy-mm-dd') // '1996-11-10'
使用 格式化当前日期。
grunt.template.today(format)
个案例中,当前日期被格式化为4位数字表示的年份。
grunt.template.today('yyyy') // '2014'