koa-views6.2.0 • Public • Published
Template rendering middleware for
npm install koa-views
koa-views is using consolidate under the hood.
NOTE: you must still install the engines you wish to use, add them to your package.json dependencies.
var views = ;// Must be used before any router is usedapp;app;
For more examples you can take a look at the tests.
If you need to simply render pages with locals, you can install
npm install koa-views-render
Then simply use it on your routes and its arguments will be passed to
var render = ;// ...app;
root: Where your views are located. Must be an absolute path. All rendered views are relative to this path
opts.autoRender: Whether to use
ctx.bodyto receive the rendered template string. Defaults to
opts.extension: Default extension for your views
Instead of providing the full file extension you can omit it.
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.
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
opts.options: These options will get passed to the view engine. This is the time to add
const app =
DEBUG environment variable to
koa-views when starting your server.