More comprehensive consolidate-style engine support for nunjucks. Should work with express, assemble, verb, generate, update, and any other app that follows consolidate conventions.
Install with npm:
$ npm install --save engine-nunjucks
var engine = require('engine-nunjucks');
Initialize Nunjucks with the given options and default settings from engine-nunjucks.
Params
options {String}Example
engine.configure([options]);
Add a global value that will be available to all templates. Note: this will overwrite any existing global called name. Returns env (the nunjucks instance) for further method chaining.
Params
name {String}returns {any} valueExample
engine.addGlobal(name, value);
Add a custom nunjucks extension. Also called "tags". This exposes the parser API and allows you to do anything you want with the template.
Params
name {String}: The name of the extension to addreturns {Function} fn: functionExample
env.addExtension(name, fn);
Asynchronously compile the given string, with the given options and callback. If no callback is passed, .compileSync is called with the given arguments.
Params
str {Object}: The string to compileoptions {Object|Function}: Options object to pass to nunjucks or callback functioncb {Function}: Callback functionreturns {undefined}Example
engine.compile('foo bar', function(err, fn) {
console.log(fn({title: 'AAA'})); //=> 'foo AAA bar'
console.log(fn({title: 'BBB'})); //=> 'foo BBB bar'
console.log(fn({title: 'CCC'})); //=> 'foo CCC bar'
});
Synchronously compile the given string with options.
Params
str {Object}: The string to compileoptions {Object}: Options object to pass to nunjucksreturns {Function}: returns the compiled functionExample
var fn = engine.compileSync('foo bar');
console.log(fn({title: 'AAA'})); //=> 'foo AAA bar'
console.log(fn({title: 'BBB'})); //=> 'foo BBB bar'
console.log(fn({title: 'CCC'})); //=> 'foo CCC bar'
Asynchronously render the given template string with locals and callback.
Params
str {String}options {Object|Function}: or callback functioncallback {Function}Example
var locals = {name: 'engine-nunjucks'};
engine.render('abc engine-nunjucks xyz', locals, function(err, html) {
console.log(html);
//=> '[foo:bar]'
})
Asynchronously or synchronously render the given str with locals and optional callback.
Params
str {Object|Function}: The string to render or compiled function.locals {Object}returns {String}: Rendered string.Example
var engine = require('engine-nunjucks');
engine.renderString('engine-nunjucks', {name: 'engine-nunjucks'}, function(err, str) {
console.log(str);
//=> 'engine-nunjucks'
});
// or
var str = engine.renderString('engine-nunjucks', {name: 'engine-nunjucks'});
console.log(str);
//=> 'engine-nunjucks'
Synchronously render the given str (or compiled function) with locals.
Params
str {String|Function}: The string to render or compiled function.locals {Object}returns {String}: Rendered string.Example
var engine = require('engine-nunjucks');
engine.renderSync('engine-nunjucks', {name: 'engine-nunjucks'});
//=> 'engine-nunjucks'
Render the contents of the file at the given filepath, with locals
and optional callback.
Params
filepath {String}options {Object|Function}: or callback functioncb {Function}Register custom filter name with the given fn. nunjucks filters are technically similar to handlebars helpers, but they're used differently in templates. This method is also aliased as .filter.
Params
name {String}: Filter namefn {Function}: Filter function.Example
engine.addFilter('foo', function(str) {
// do stuff to `str`
return str;
});
Register multiple template filters. Also aliased as .filters.
Params
filters {Object|Array}: Object, array of objects, or glob patterns.Example
engine.addFilters({
foo: function() {},
bar: function() {},
baz: function() {}
});
Register an async filter function. Also aliased as .asyncFilter.
Params
name {String}: Filter name.fn {Function}: Filter functionExample
engine.addAsyncFilter('upper', function(str, next) {
next(null, str.toUpperCase());
});
Register multiple async template filters. Also aliased as .asyncFilters.
Params
filters {Object|Array}: Object, array of objects, or glob patterns.Example
engine.addAsyncFilters({
foo: function() {},
bar: function() {},
baz: function() {}
});
Get a previously registered filter.
Params
name {String}: Filter namereturns {Function}: Returns the registered filter function.Example
var fn = engine.getFilter('foo');
Get a previously registered async filter.
Params
name {String}: Filter namereturns {Function}: Returns the registered filter function.Example
var fn = engine.getAsyncFilter('foo');
These are the actual default options used. These can be overridden with custom values on any of the methods.
var defaults = {
ext: '.html',
name: 'nunjucks',
base: 'templates',
throwOnUndefined: true,
autoescape: false,
watch: false
};
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
(This document was generated by verb-generate-readme (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)
To generate the readme and API documentation with verb:
$ npm install -g verb verb-generate-readme && verb
Install dev dependencies:
$ npm install -d && npm test
Jon Schlinkert
Copyright © 2016, Jon Schlinkert. Released under the MIT license.
This file was generated by verb, v0.9.0, on July 27, 2016.