An expressjs plugin for handling DustJS view rendering. [dustjs-helpers] (https://github.com/linkedin/dustjs-helpers) are included by default in this module.
var express = require('express');
var dustjs = require('adaro');
var app = express();
app.engine('dust', dustjs.dust({ ... });
app.set('view engine', 'dust');
// For rendering precompiled templates:
// app.engine('js', dustjs.js({ ... ));
// app.set('view engine', 'js');
Config options can be used to specify dust helpers, enabled/disable caching, and custom file loading handlers.
Dust understands partials, but doesn't understand layouts. Layouts allow you to skin your application in different ways without having to rewrite all of your partials.
For example, here are two Dust templates: a layout, and a content page. The
layout includes a special partial with the dynamic name {_main}
. The content
page has no knowledge of layout; it is itself just a partial.
<html>
<body>
{>"{_main}"/}
</body>
</html>
<div>Hello!</div>
Using layout
, when a template is rendered, a layout can be
specified or disabled. As long as the layout template includes the dynamic partial via
{>"{_main}"/}
the template you asked for will be wrapped in the specified
layout.
// Use alternate layout
dust.render('index', { layout: 'myLayout' }, ...);
// Disable layout altogether
dust.render('index', { layout: false }, ...);
<html>
<body>
<div>Hello!</div>
</body>
</html>
A helper module must either:
- Conform to the API established by [dustjs-helpers] (https://github.com/linkedin/dustjs-helpers) provided by LinkedIn or
- Export a function which accepts a single argument (being dust itself). Such files should generally be designed for use on both client and server.
Client and Server Compatible
(function (dust) {
// Add helpers
}(typeof exports !== 'undefined' ? module.exports = require('dustjs-linkedin') : dust));
Alternate API
module.exports = function (dust) {
// Add helpers
};
Set to true to enable dust template caching, or false to disable. If a custom onLoad handler is defined, caching is disabled and assumed to be handled by the client.
Define a file read handler for use by dust in loading files.
dustjs.onLoad = function (name, context, callback) {
// Custom file read/processing pipline
callback(err, str);
}
app.engine('dust', dustjs.dust({ cache: false }));
app.set('view engine', 'dust');
###FAQ
You need to add these libs as dependencies in your app's own package.json.
We intentionally didn't add the dependency to this module's package.json such that we didn't want to be in control of it or its versions on behalf of apps. That way app owners can manage the dependency however they see fit and this module can just "enhance" the existing library. It does assume the version of dust.js doesn't change template loading or streaming behavior, but beyond that compatibility should be fine. If there IS a major version change to dust APIs we'll be sure to manage those changes here, so the upgrade/handling of it is transparent.