Dust.js load template from filesystem in Node.js
Asked Answered
V

2

0

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.

Vince answered 5/5, 2012 at 7:16 Comment(2)
Are you trying to just use Dust templating rather than say, Jade? If so, this link describes doing it with consolidate.js : spalatnik.com/blog/?p=54Glair
Thx, Trevor, that's more like what I'm looking for.Vince
A
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)
    })
})
Allyl answered 5/5, 2012 at 8:25 Comment(1)
it just looks a bit weird, that I have to access file system manually to get template. Was hoping there is a bit more abstraction way of doing it.Vince
A
0

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

Aloes answered 15/6, 2012 at 0:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.