1.0.1 • Published 2 years ago

nunjucks-render v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Nunjucks Render

Build NPM Version NPM Downloads

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

NameTypeDescription
environmentEnvironmentInstance of Nunjucks environment
templatePathStringPath 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.