koa-nunjucks-2
Lightweight Koa middleware for Nunjucks.
Installation
npm install --save koa-nunjucks-2
NOTE: v3 requires Koa 2 or later. If you're using Koa 1, use v2 of this package.
Usage
Example
const Koa = ;const app = ;const koaNunjucks = ;const path = ; app; app;
Config Options
- ext (default: 'njk'): Extension that will be automatically appended to the file name in
ctx.render
calls. Set to a falsy value to disable. - path (default: current directory): Path to the templates. Also supports passing an array of paths.
- writeResponse (default: true): If true, writes the rendered output to
response.body
. - functionName (default: 'render'): The name of the function that will be called to render the template.
- nunjucksConfig: Object of Nunjucks config options.
- configureEnvironment: A function to modify the Nunjucks environment. See the Extending Nunjucks section below for usage.
Global Template Variables
Use ctx.state to make a variable available in all templates.
Extending Nunjucks
Use the configureEnvironment
config option to define a function which will receive a Nunjucks Environment as its argument. This allows you to define custom filters, extensions etc.
app;