nunjucks-render
v1.0.1
Published
Nunjucks Extension for rendering templates
Downloads
3
Readme
Nunjucks Render
This extensions allows you to render another template inside an template. This works like the include tag, but you can supply parameters to the template and optionally access the Nunjucks context.
How to install it?
$ npm install nunjucks-render
How to use it?
import { RenderExtension } from 'nunjucks-render';
env.addExtension(
'render-extension',
new RenderExtension({
environment: env,
templatePath: 'path/to/template/root',
}),
);
Parameters for RenderExtension constructor
| Name | Type | Description |
|--------------| --- |----------------------------------|
| environment | Environment
| Instance of Nunjucks environment |
| templatePath | String
| Path to template files |
Usage in Templates
To render another template in your template use the render
tag. The syntax is
{% render <template file>, <data object> [, <options>] %}
The options
object is optional. With { includeContext: true }
, you have access to the Nunjucks Context inside the
template. Use the context
variable for access the context.
Template subtemplate.html.njk
:
This is the param value: {{ param }}.
{% render 'subtemplate.html.njk', { param: 'param value' }, { includeContext: true } %}
This is the param value: param value.