Better templating with LinkedIn's dust fork.
Dustin extends dust with some missing functionality like cache control and formatting option for white space preservation.
It also provides a convenient express engine (dust.__express
).
This package also includes dustjs-helpers
alongside some useful helpers.
npm i dustin --save
var dustin = require("dustin")
var dust = dustin({
cache: true,
views: "/",
helpers: "helpers/*.js",
whiteSpace: true
})
If false, every dust.render()
will purge the cache.
It is especially useful for development, when changes to a template should be
reflected in the browser on reload.
Partials will resolve to this folder. It helps so you don't have to write full template paths all the time.
A glob pattern for user helpers to extend the dust.helpers
object.
A helper should export a function with one or two arguments:
module.exports = function( helpers, dust ){
helpers.something = function(chunk, context, bodies, params){}
}
An express engine.
Hook it to express like this:
var dustin = require("dustin")
var engine = dustin({
cache: false,
views: "app/views",
helpers: "app/helpers/*.js",
whiteSpace: true
})
app.engine("dust", engine.__express)
app.set("view engine", "dust")
app.set("views", "app/views")
app.set("view cache", false)
Returns a template name according to the options you passed to dustin
Returns an absolute path concatenated from the cwd, the template dir you passed to dustin and the name argument with the .dust extension
var dustin = require("dustin")
dustin.client("destination folder", "resolve path", {
dust: true,
user: "",
custom: ""
})
Client side scripts will be copied here.
Client templates are loaded like this:
script.src = /^(https?:)?\/\/?/.test(template)
? template
: ("RESOLVE_PATH" + "/" + template + ".js").replace(/\/+/g, "/")
Set the resolve path to a template root.
a variable name for the key. defaults to $key
a variable for the value. defaults to $value
the object to iterate over if not provided context.current()
will be used
params are prefixed with a $
so it's less likely they clash with context members
@example
Context
"ooo": {
"a": {
"1": "1"
},
"b": {
"2": "2"
}
}
Template
{@for var="asd" value="qwe" $in=ooo}
{asd}
{@for:qwe}
{$key} - {$value} {~n}
{/for}
{/for}
Output
a1 - 1
b2 - 2
Embed a file from the file system into the template.
The file's source.
Render a partial's body with the macro's params (except the partial).
The template name of a partial
This simply sets the context to the head of the stack. It helps cutting down on typing accessors.
context
"someObject": {
"a": "hello",
"b": "hi"
}
template
{@with:someObject}
{a}{~n}
{b}
{/with}
Output
hello
hi
The same as dust.render, but instead of a string it calls done(err, out)
with a document fragment.
MIT