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.
$ npm install nunjucks-render
import { RenderExtension } from 'nunjucks-render';
env.addExtension(
'render-extension',
new RenderExtension({
environment: env,
templatePath: 'path/to/template/root',
}),
);
Name | Type | Description |
---|---|---|
environment | Environment |
Instance of Nunjucks environment |
templatePath | String |
Path to template files |
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 }}.
You can render this sub template like the following in another template:
{% render 'subtemplate.html.njk', { param: 'param value' }, { includeContext: true } %}
The output will be:
This is the param value: param value.