Created
January 7, 2014 15:32
-
-
Save seriousManual/8301058 to your computer and use it in GitHub Desktop.
dustjs: proposal for the persistent storage of compiled templates
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
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; | |
})(); |
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
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