The grunt API / grunt.template
October 1, 2012 ยท View on GitHub
Grunt homepage | Documentation table of contents
The grunt API / grunt.template
Underscore.js template processing and other template-related methods.
Template strings can be processed manually using the provided template functions. In addition, many tasks and helpers automatically expand <% %> style template strings specified inside the grunt.js gruntfile when used as file paths and banners.
See the template lib source for more information.
The template API
grunt.template.process
Process an Underscore.js template string. If data is omitted, the entire config object is used. Templates are processed recursively until there are no more templates to process.
Inside templates, the grunt object is exposed as grunt so that you can do things like <%= grunt.template.today('yyyy') %>. Note that if the data object has a grunt property, it will prevent this from working.
If mode is omitted, <% %> style template delimiters will be used. If mode is "init", {% %} style template delimiters will be used (this is specifically used by the init task).
grunt.template.process(template, data, mode)
In this example, the baz property is processed recursively until there are no more <% %> templates to process.
var obj = {
foo: 'c',
bar: 'b<%= foo %>d',
baz: 'a<%= bar %>e'
};
grunt.template.process('<%= baz %>', obj) // 'abcde'
grunt.template.delimiters
Set Underscore.js template delimiters manually, in case you need to use grunt.utils._.template manually. You probably won't need to call this, because you'll be using grunt.template.process which calls this internally.
If mode is omitted, <% %> style template delimiters will be used. If mode is "init", {% %} style template delimiters will be used (this is specifically used by the init task).
grunt.template.delimiters(mode)
Template Helpers
grunt.template.date
Format a date using the dateformat library.
grunt.template.date(date, format)
In this example, a specific date is formatted as month/day/year.
grunt.template.date(847602000000, 'yyyy-mm-dd') // '1996-11-10'
grunt.template.today
Format today's date using the dateformat library.
grunt.template.today(format)
In this example, today's date is formatted as a 4-digit year.
grunt.template.today('yyyy') // '2012'
(somebody remind me to update this date every year so the docs appear current)