engine-nunjucks
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
Install with npm:
$ npm install --save engine-nunjucks
Usage
var engine = ;
API
.configure
Initialize Nunjucks with the given options
and default settings from engine-nunjucks
.
Params
options
{String}
Example
engine;
.addGlobal
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}value
Example
engine;
.addExtension
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
: function
Example
env;
.compile
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;
.compileSync
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 function
Example
var fn = engine;console; //=> 'foo AAA bar'console; //=> 'foo BBB bar'console; //=> 'foo CCC bar'
.render
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
.renderString
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 = ;engine;// orvar str = engine;console;//=> 'engine-nunjucks'
.renderSync
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 = ;engine;//=> 'engine-nunjucks'
.renderFile
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}
.addFilter
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;
.addFilters
Register multiple template filters. Also aliased as .filters
.
Params
filters
{Object|Array}: Object, array of objects, or glob patterns.
Example
engine;
.addAsyncFilter
Register an async filter function. Also aliased as .asyncFilter
.
Params
name
{String}: Filter name.fn
{Function}: Filter function
Example
engine;
.addAsyncFilters
Register multiple async template filters. Also aliased as .asyncFilters
.
Params
filters
{Object|Array}: Object, array of objects, or glob patterns.
Example
engine;
.getFilter
Get a previously registered filter.
Params
name
{String}: Filter namereturns
{Function}: Returns the registered filter function.
Example
var fn = engine;
.getAsyncFilter
Get a previously registered async filter.
Params
name
{String}: Filter namereturns
{Function}: Returns the registered filter function.
Example
var fn = engine;
Default options
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;
About
Related projects
- assemble: Get the rocks out of your socks! Assemble makes you fast at creating web projects… more | homepage
- generate: Command line tool and developer framework for scaffolding out new GitHub projects. Generate offers the… more | homepage
- update: Be scalable! Update is a new, open source developer framework and CLI for automating updates… more | homepage
- verb: Documentation generator for GitHub projects. Verb is extremely powerful, easy to use, and is used… more | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Building docs
(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
Running tests
Install dev dependencies:
$ npm install -d && npm test
Author
Jon Schlinkert
License
Copyright © 2016, Jon Schlinkert. Released under the MIT license.
This file was generated by verb, v0.9.0, on July 27, 2016.