Skip to content

Instantly share code, notes, and snippets.

@skeate
Last active August 29, 2015 13:58
Show Gist options
  • Save skeate/9948073 to your computer and use it in GitHub Desktop.
Save skeate/9948073 to your computer and use it in GitHub Desktop.
Underscore template precompiling script. Compiles any .html files into templates.js. Include the file and templates can be run with templates["filename"](data).
var _ = require("underscore")._;
var fs = require("fs");
fs.readdir(".", function(err,files){
fs.writeFileSync('./templates.js',"templates={};"+
files
.filter(function(f){return f.substr(-5) == ".html"})
.map(function(f){return {name:'templates["'+f.substr(0,f.length-5)+'"]',source:fs.readFileSync(f).toString()};})
.map(function(s){return s.name+"="+_.template(s.source).source+";";})
.join('')
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment