Skip to content

Instantly share code, notes, and snippets.

@terribleplan
Created June 23, 2014 02:43
Show Gist options
  • Save terribleplan/c9033e963b0019cf81f7 to your computer and use it in GitHub Desktop.
Save terribleplan/c9033e963b0019cf81f7 to your computer and use it in GitHub Desktop.
Grunt render-handlebars
//I'll probably turn this into an actual plugin at some point, but for now this is a simple way to compile handlebars templates statically
grunt.registerMultiTask('render-handlebars', function () {
var done = this.async();
var inputFile = this.data["inputFile"],
outputFile = this.data["outputFile"],
removeInput = !!(this.data["removeTemplate"] || false),
templateName = this.data["templateName"] || "index";
try {
var Handlebars = require('handlebars');
Handlebars.partials = require(inputFile);
grunt.file.write(outputFile, Handlebars.partials[templateName]({}));
grunt.log.writeln("File " + outputFile + " written from ['" + templateName + "'] of " + inputFile);
if (!removeInput) {
done(true);
return;
}
grunt.file.delete(inputFile);
grunt.log.writeln(inputFile + " deleted.");
done(true);
} catch (e) {
grunt.log.error(e.message);
done(false);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment