Max
Max

Reputation: 211

Dust.js load template from filesystem in Node.js

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.

Upvotes: 0

Views: 3263

Answers (2)

CoolestAth
CoolestAth

Reputation: 31

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

Upvotes: 0

Raynos
Raynos

Reputation: 169401

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)
    })
})

Upvotes: 1

Related Questions