Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.
/ koajs-nunjucks Public archive

Koa Nunjucks rendering middleware

License

Notifications You must be signed in to change notification settings

DigitalIO/koajs-nunjucks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Koa.js Nunjucks

NPM Version Node Version License

Installation

$ npm install koajs-nunjucks

Middleware Setup

First, require koajs-nunjucks. It returns a function that will return a middleware generator.

var KoajsNunjucks = require('koajs-nunjucks');
var fMiddleware = KoajsNunjucks('', {});

The function takes two arguments. They are passed in the same order to the nunjucks.configure() function. Next, "use" the middleware function (before any other middleware that attempts to use it).

var app = require('koa')();
app.use( fMiddleware );

Usage

The render function is attached to the Koa context. It is availiable in other middleware functions directly as "this". The function renders the view and automatically sets it as the response body.

yield this.render('home.nj', { username: 'Bob' });

The render function takes two arguments as defined by nunjucks.render. It does NOT take a callback.

In additon to the render function, the enviorment is also attached to the context. Every request creates a fresh enviorment.

this.nunjucks

License

MIT