Skip to content

Instantly share code, notes, and snippets.

@danielstocks
Created December 24, 2011 09:32
Show Gist options
  • Save danielstocks/1516995 to your computer and use it in GitHub Desktop.
Save danielstocks/1516995 to your computer and use it in GitHub Desktop.
// dust.js template pre-compilation
var template = dust.compile("Hello {name}!");
// compiles to this string:
//
// '(function() {
// dust.register("demo", body_0);
//
// function body_0(chk, ctx) {
// return chk.write("Hello ").reference(ctx.get("name"), ctx, "h").write("!");
// }
// return body_0;
// })();'
//
// This can easily be written to a file like hello.js
// Hogan.js template pre-compilation
var template = hogan.compile("Hello {name}!");
// Compiles to an object.
//
// { text: 'Hello {name}!', r: [Function] } }
//
// I can do template.r.toString() to write the function to a hello.js file,
// but the function calls inside this function depend on the HoganTemplate
// prototype. And the reference to 'this' is lost, when extracting the function.
//
// Applying the right context manually
var render = template.r.toString();
render.apply(new HoganTemplate, [{name: "world"}]);
// This works, as the render function now has access to the "HoganTemplate" prototype
// but it seems a bit clumsy? Got any other ideas?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment