Skip to content

Instantly share code, notes, and snippets.

@mattsta
Created January 23, 2012 07:29
Show Gist options
  • Save mattsta/1661393 to your computer and use it in GitHub Desktop.
Save mattsta/1661393 to your computer and use it in GitHub Desktop.
server-side stdin/stdout mustache template compiler for hogan.js
#!/bin/env node
/*****************
* After getting your compiled template, you re-instantiate it client side by:
* var myTemplate = new Hogan.Template(compiledTemplateEvaledFunction);
* var renderedTemplate = myTemplate.render({field1: "someVal"});
*****************/
/* open le stdin */
var stdin = process.openStdin();
stdin.setEncoding('utf8');
/* read stdin into inputBuffer */
var inputBuffer = "";
stdin.on("data", function(chunk) {
inputBuffer += chunk;
});
/* when EOF comes in, write the compiled JS to stdout */
stdin.on("end", function() {
var hogan = require("hogan.js"); /* make sure you `npm install hogan.js` */
var compiledTemplate = hogan.compile(inputBuffer, {asString: true});
process.stdout.write(compiledTemplate);
console.log(""); /* insert a newline just for fun */
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment