node package manager

koa-views

Template rendering middleware for koa

koa-views

Template rendering middleware for koa.

Note: This master branch is for koa's upcoming version koa@2. Take a look at the v1.x branch for koa@1.

$ npm install koa-views@next

As of right now, koa-views is using consolidate under the hood.

List of supported engines

var views = require('koa-views');
 
// Must be used before any router is used 
app.use(views(__dirname + '/views', {
  map: {
    html: 'underscore'
  }
}));
 
app.use(async function (ctxnext) {
  ctx.state = {
    session: this.session,
    title: 'app'
  };
 
  await ctx.render('user', {
    user: 'John'
  });
});

For more examples take a look at the tests

  • root: Where your views are located. All views you render() are relative to this path.
  • opts (optional)
  • opts.extension: Default extension for your views
// instead of this 
yield this.render('user.jade')
// you can 
yield this.render('user')
  • opts.map: map extension to an engine
app.use(views(__dirname, { map: {html: 'nunjucks' }}))
 
// render `user.html` with nunjucks 
app.use(async function (ctx) {
  await ctx.render('user.html')
})

Set the DEBUG environment variable to koa-views when starting your server.

$ DEBUG=koa-views

MIT