Dust.js load template from filesystem in Node.js

女生的网名这么多〃 提交于 2019-12-13 05:56:27

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!