Skip to content

mgascoyne/nunjucks-render

Repository files navigation

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

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 }}.

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.

About

Nunjucks Extension for rendering templates

Resources

Stars

Watchers

Forks

Packages

No packages published