问题
I'm trying to load template from file system using node.js and can't find the way. So far I have:
exports.index = function(req, res){
var compiled = dust.compile("Hello {name}!", "intro");
dust.loadSource(compiled);
dust.render("intro", {name: "Fred"}, function(err, out) {
res.write(out);
res.close();
});
};
Is there is a way to replace: "Hello {name}!" with file name? Should it be HTML or JS file? Also if that's not really great way on rendering templates let me know, I'm new to Node.js trying to pick up best practices.
回答1:
fs.readFile(dustFile, function (err, file) {
var name = fileName.split(".")[0]
var fn = dust.compileFn(file.toString(), name)
fn({ name: "fred" }, function (err, out) {
res.end(out)
})
})
回答2:
This should help you. dust fs.
This is a simplified interface to use templates from filesystem with {dust} using Node.js.
https://github.com/jheusala/dustfs
来源:https://stackoverflow.com/questions/10459770/dust-js-load-template-from-filesystem-in-node-js