|
/* |
|
* |
|
* Task: jsthb |
|
* Description: Compile Handlebars templates to JST file |
|
* Contributor(s): @mheap |
|
* |
|
*/ |
|
|
|
module.exports = function(grunt) { |
|
|
|
var file = grunt.file, |
|
log = grunt.log; |
|
|
|
var _ = grunt.utils._; |
|
|
|
grunt.registerMultiTask("jsthb", |
|
"Compile underscore templates to JST file", function() { |
|
|
|
var options = grunt.config("jsthb", this), |
|
namespace = options.namespace || "JST", |
|
settings = options.templateSettings || null, |
|
files = file.expand(this.data); |
|
|
|
// Create JST file. |
|
file.write(this.target, grunt.helper("jsthb", files, namespace, settings)); |
|
|
|
// Fail task if errors were logged. |
|
if (grunt.errors) { return false; } |
|
|
|
// Otherwise, print a success message. |
|
log.writeln("File \"" + this.target + "\" created."); |
|
}); |
|
|
|
grunt.registerHelper("jsthb", function(files, namespace, templateSettings) { |
|
// Ensure we get the underscore from the node_modules folder |
|
var Handlebars = require("handlebars"); |
|
|
|
// Comes out looking like this["JST"] = this["JST"] || {}; |
|
var contents = "(function() {\n var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};\n"; |
|
|
|
// Compile the template and get the function source |
|
contents += files ? files.map(function(filepath) { |
|
var templateFunction = 'templates[\'' + filepath + '\'] = template(' + Handlebars.precompile(file.read(filepath)) + ');\n'; |
|
|
|
return templateFunction; |
|
}).join("\n\n") : ""; |
|
|
|
return contents + "})();"; |
|
}); |
|
|
|
}; |