koa-views
Template rendering middleware for koa.
Note: This master branch is for koa's upcoming version
koa@2
. Take a look at thev1.x
branch forkoa@1
.
Installation
$ npm install koa-views@next
Templating engines
koa-views
is using consolidate under the hood.
Example
var views = ; // Must be used before any router is usedapp; app;
For more examples you can take a look at the tests.
API
views(root, opts)
root
: Where your views are located. Must be an absolute path. All rendered views are relative to this pathopts
(optional)opts.extension
: Default extension for your views
Instead of providing the full file extension you can omit it.
app
vs.
app app
opts.map
: Map a file extension to an engine
In this example, each file ending with .html
will get rendered using the nunjucks
templating engine.
app // render `user.html` with nunjucksapp
opts.engineSource
: replace consolidate as default engine source
If you’re not happy with consolidate or want more control over the engines, you can override it with this options. engineSource
should
be an object that maps an extension to a function that receives a path and options and returns a promise. In this example templates with the foo
extension will always return bar
.
app app
opts.options
: These options will get passed to the view engine. This is the time to addpartials
andhelpers
etc.
const app =
Debug
Set the DEBUG
environment variable to koa-views
when starting your server.
$ DEBUG=koa-views