Nunjucks support for JSTransformers.
npm install jstransformer-nunjucks
const jstransformer = require('jstransformer')
const nunjucks = jstransformer(require('jstransformer-nunjucks'))
nunjucks.render('Hello, {{ name }}!', {name: 'World'}).body
//=> 'Hello, World!'
const options = {
filters: { repeat: (str, num) => str.repeat(n || 2) }
};
nunjucks.render(
'{{ "Hello, " | repeat(echoes + 1) }}{{ name }}!',
options,
{name: 'World', echoes: 2}
).body
//=> 'Hello, Hello, Hello, World!'
See the JSTransformers documentation for other API methods.
By default, this transformer matches the .njk and .nunjucks input file extensions, and outputs .html.
Many of the API methods accept an options
dictionary object. The following option keys are supported:
-
filename
: The filename and path of the Nunjucks template being compiled. Default is null, which disables any imports or includes using relative path names. -
root
: The base path used to configure the Nunjucks environment. This defines the highest-level directory that can be searched for templates and macros. Any import or include references to files outside this root directory will fail. Default is to use the parent directory of thefilename
path, if it is specified, ornull
(which causes Nunjucks to default to the current working directory). -
path
: Alternative name forroot
. -
filters
: A set of custom Nunjucks filters to add. The value offilters
should be a dictionary object where the keys are the filter names to use in the templates. The dictionary values define the filter functions, either as JavaScript function objects, or as the name of a Node module (as a string). If you specify a module name, that module's default export will be used as the filter function. -
extensions
: A set of Nunjucks extensions to add. The value ofextensions
should be a dictionary object where the keys are the extension names to use in the templates. The dictionary values define the extension functions, either as JavaScript function objects, or as the name of a Node module (as a string). -
globals
: A set of global variables available to all templates. The value ofglobals
is a dictionary object defining the keys and values of the global data properties.
MIT