Closed arden closed 13 years ago
Express exposes a synchronous rendering API that's not compatible with dust. However, dust supports helpers, partials, layouts, etc out of the box so there's no need to interface with the express view system.
First you need to tell dust where to look for templates:
dust.onLoad = function(name, cb) {
fs.readFile(path_to_templates + name, 'utf8', cb);
};
Then you can define some global context helpers:
var helpers = dust.makeBase({
foo: function(chk, ctx, bodies) { ... },
bar: ...
});
Finally, inside your express action just call dust's render function directly:
dust.render('login.html', helpers.push(context), function(err, out) {
...
res.send(out);
});
i'm found a way use dustjs with Meryl(http://coffeemate.github.com/meryl/)
var meryl = require('../../index'), fs = require('fs'), path = require('path'), dust = require('dust');
var dataStore = {
posts: [
{
key: 1,
title: "post 1",
date: "01/19/10"
},
{
key: 2,
title: "post 2",
date: "01/14/10",
content: "This is the post 2"
},
{
key: 3,
title: "post 3",
date: "04/2/10",
content: "This is the post 3"
}
]
};
var TEMPLATE_DIR = 'templates', TEMPLATE_EXT = '.html';
function initDust(onInit) { fs.readdir(TEMPLATE_DIR, function (err, filenames) { if (err) { throw err; } var filesRead = 0, templateName, pattern = new RegExp('\' + TEMPLATE_EXT + '$');
filenames.forEach(function (filename) {
if (pattern.test(filename)) {
fs.readFile(path.join(TEMPLATE_DIR, filename), function (err, data) {
if (err) {
throw err;
}
templateName = filename.replace(pattern, '');
dust.loadSource(dust.compile(data.toString(), templateName));
console.log(templateName + ' template prepared.');
filesRead += 1;
if (filenames.length === filesRead) {
onInit();
}
});
}
});
}); }
meryl .plug(function (req, resp, next) { resp.render = function (templatename, data) { dust.render(templatename, data, function (err, output) { if (err) { throw err; } resp.send(output); }); }; next(); }) .get('/', function (req, resp) { resp.render('main', dataStore); });
initDust(function () { meryl.run(); console.log('listening...'); });
and i'm can do it like this with Express framework as Meryl?
there's any way that use dustjs with express framework?
https://github.com/visionmedia/express