2.0.3 • Published 8 years ago

koa-nunjucks2 v2.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

koa-nunjucks2 License

NPM

koa2 middleware for Nunjucks.

Installation

$ npm install koa-nunjucks2 --save

Middleware Setup

First, require koa-nunjucks2. It returns a function that will return a middleware async function.

let koaNunjucks2 = require('koa-nunjucks2');
let koaNunjucks2Middleware = koaNunjucks2('views', {autoescape: true});

The function takes four arguments:

  • templatesPath (default: current working directory): Where is the templates, will pass to nunjucks.configure() function.
  • nunjucksOptions: Object of Nunjucks config options.
  • extConfig (default: {suffix: '.html', contentType: 'text/html; charset=UTF-8', renderToResponseBody: true}): Extended config of this middleware.
  • callback: The environment will pass to the callback function, it can be used to add filters and so on.

Next, "use" the middleware function (before any other middleware that attempts to use it).

const Koa = require('koa');
const app = new Koa();
app.use(koaNunjucks2Middleware);

Usage

The render function is attached to the Koa context. The function renders the view and automatically sets it as the response body if "renderToResponseBody" set true.

await ctx.render('home', { username: 'Whatever name' });

The render function takes two arguments as defined by nunjucks.render. It does NOT take a callback, but return a promise. If "renderToResponseBody" set false, the render result will return by promise.

If you want to make a variable available in all templates, use koa ctx.state.

License

MIT

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago