Skip to content

Instantly share code, notes, and snippets.

@seriousManual
Created January 7, 2014 15:32
Show Gist options
  • Save seriousManual/8301058 to your computer and use it in GitHub Desktop.
Save seriousManual/8301058 to your computer and use it in GitHub Desktop.
dustjs: proposal for the persistent storage of compiled templates
var dust = require('dustjs-linkedin');
//whitespaces added for clarification
module.exports = (function(){
dust.register("intro",body_0);
function body_0(chk,ctx){
return chk.write("Hello ").reference(ctx._get(false, ["name"]),ctx,"h").write("!");
}
return body_0;
})();
var dust = require('dustjs-linkedin');
var templateName = 'intro';
var serializedTemplate = require('./' + templateName);
function createCallableTemplate(name, tmpl) {
return function(context, callback) {
var master = callback ? new dust.Stub(callback) : new dust.Stream();
dust.nextTick(function() {
if(typeof tmpl === 'function') {
tmpl(master.head, dust.Context.wrap(context, name)).end();
}
else {
dust.onError(new Error('Template [' + name + '] cannot be resolved to a Dust function'));
}
});
return master;
};
}
var usableTemplate = createCallableTemplate(templateName, serializedTemplate);
usableTemplate({name:'fooooo'}, function(error, result) {
console.log(result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment