Template rendering middleware for koa (using nunjucks).
$ npm install koa-view
const view = require('koa-view');
// Must be used before any router is used
app.use(view(__dirname + '/views'));
app.use(async function (ctx) {
ctx.state = {
session: ctx.session,
title: 'app'
};
await ctx.render('user', {
user: 'Coder'
});
});
More examples: tests
root
: (defaultviews
) Views location. All view yourender()
are relative to this path.opts
nunjucks configure optsopts.ext
: (defaulthtml
) Extension for your view
// instead of this
await ctx.render('user.html')
// you can
await ctx.render('user')
To use koa-view
with koa@1, please use koa-view 1.x.
npm install koa-view@1 --save
MIT