Created
December 24, 2011 09:32
-
-
Save danielstocks/1516995 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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