This Grunt plugin interpolates template files with any data you provide and saves the result to another file.
Since grunt.template.process is used for the templating, this Grunt plugin is very lightweight and doesn’t have any dependencies (other than Grunt itself).
This plugin requires Grunt v0.4.0+.
If you haven’t used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you’re familiar with that process, you may install this plugin with this command:
npm install grunt-template--save-dev
The template task
In your project’s Gruntfile, add a section named template to the data object passed into grunt.initConfig().
// Task-specific options go here
// Target-specific options go here
// Target-specific file lists go here
The options property accepts the following options:
Type: Object or Function
This object contains the data that will be used while interpolating the template files. If you pass a function instead, it will be called when grunt-template needs the template data (lazy evaluation). This is useful if you want to load data from a file that is generated by another Grunt task, for example.
Type: String or Function
This is the delimiters' name that will be used to interpolate and evaluate code. A function that returns this name can be used too.
This property is useful when you want to generate JSP/ERB like code and you need the default interpolation delimiters to be <% and %>. See below for an example.
Here’s a practical example of grunt-template. Here, the file src/post.html.tpl is loaded, then parsed as a template using the provided data object (with title, author and content properties), and finally the result is saved as dist/post.html.
<!DOCTYPE html><title><%- title %></title><h1><%- title %>, by <%- author %></h1><p><%- content %></p>