Precompile templates to a file and/or expose the templates through a module.
This is an awful lot like the existing grunt-contrib-jst grunt task. But it has a few
differences. One is that it supports underscore
, lodash
and ejs
template providers. The real
key feature is that it allows you to compile these templates into a module that exposes each compiled template from an exports
property.
What this means is that if you provide the following configuration:
compile_module: {
files: {
"tmp/module_jst.js": ["test/fixtures/template.html"]
},
options: {
module: true
}
}
You would end up with an entry in module_jst.js
that looks like:
exports["tmp/module_jst.js"] = function(obj){...
I know. You're wondering why do such a thing? Doesn't the contrib-jst
offer an AMD wrapper? Yes, it does, but that doesn't help if you want
to get at the template from the server. As well, if you use browserify, this will play much better with
the compiled browserified (is that word?) output.
So there you have it. Browserify and server happiness for underscore/lodash and EJS templates. And backward compatibility with contrib-jst
.
And one more thing: prettify uses beautify instead. Let's try that again. The prettify option in the original used some very simple code to pretty things up. Instead it now uses node-beautify.
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, install this plugin with this command:
npm install grunt-template-module --save-dev
Run this task with the grunt template-module
command.
This task is a [multi task][] so any targets, files and options should be specified according to the [multi task][] documentation. [multi task]: https://github.com/gruntjs/grunt/wiki/Configuring-tasks
Type: 'Boolean' Default: true
When true, the module behavior described above will be delivered with love and chocolate.
Type 'String' Default: underscore
The name of the template engine to use. Allowable values are underscore
lodash
and ejs
Type: String
Default: 'JST'
The namespace in which the precompiled templates will be asssigned. Use dot notation (e.g. App.Templates) for nested namespaces. This is not used when module is set to true.
Type: function
Default: null
This option accepts a function which takes one argument (the template filepath) and returns a string which will be used as the key for the precompiled template object. The example below stores all templates on the default JST namespace in capital letters.
options: {
processName: function(filename) {
return filename.toUpperCase();
}
}
Type: Object
Default: null
The settings passed to the template engine when compiling templates. The options are template engine specific.
template-module: {
compile: {
options: {
templateSettings: {
interpolate : /\{\{(.+?)\}\}/g
}
},
files: {
"path/to/compiled/templates.js": ["path/to/source/**/*.html"]
}
}
}
Type: boolean
Default: false
When doing a quick once-over of your compiled template file, it's nice to see an easy-to-read format. This will accomplish that.
options: {
prettify: true
}
Type: boolean
Default: false
With Require.js and a pre-compiled template.js you want the templates to be wrapped in a define. This will wrap the output in:
define(function() {
//Templates
return this["NAMESPACE"];
});
Example:
options: {
amdWrapper: true
}
- This is not used when module is set to true.*
template-module: {
compile: {
options: {
templateSettings: {
interpolate : /\{\{(.+?)\}\}/g
}
},
files: {
"path/to/compiled/templates.js": ["path/to/source/**/*.html"]
}
}
}
- 2012-12-24 v0.1.0 Initial release