Template strings can be processed manually using the provided template functions. In addition, the config.get method (used by many tasks) automatically expands <% %>
style template strings specified as config data inside the Gruntfile
.
Process a Lo-Dash template string. The template
argument will be processed recursively until there are no more templates to process.
The default data object is the entire config object, but if options.data
is set, that object will be used instead. The default template delimiters are <% %>
but if options.delimiters
is set to a custom delimiter name, those template delimiters will be used instead.
grunt.template.process(template [, options])
Inside templates, the grunt
object is exposed so that you can do things like <%= grunt.template.today('yyyy') %>
. Note that if the data object already has a grunt
property, the grunt
API will not be accessible in templates.
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 %>', {data: obj}) // 'abcde'
Set the Lo-Dash template delimiters to a predefined set in case you grunt.util._.template
needs to be called manually. The config
delimiters <% %>
are included by default.
You probably won't need to use this method, because you'll be using grunt.template.process
which uses this method internally.
grunt.template.setDelimiters(name)
Add a named set of Lo-Dash template delimiters. You probably won't need to use this method, because the built-in delimiters should be sufficient, but you could always add {% %}
or [% %]
style delimiters.
grunt.template.addDelimiters(name, opener, closer)
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'
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') // '2014'
(somebody remind me to update this date every year so the docs appear current)