koa-underscore-templates
v0.2.0
Published
Render underscore.js templates with Koa
Downloads
15
Readme
koa-underscore-templates
Render underscore.js templates with Koa.
Works with Koa 2.x or newer.
Getting started
Add this package as a middleware for your application:
const views = require('koa-underscore-templates');
app.use(views(__dirname + '/views', {
// optional options here...
}));
Then call ctx.render
to render the page:
app.use(async (ctx) => {
await ctx.render('index.html');
});
Documentation
Middleware
The middleware takes the path of the root directory of the templates as the first parameter. The second parameter is optional, and can contain the following optional options:
extension
(default:.html
)
If non-null, then this will be appended to the file name. For instance,extension: .html
would makectx.render("index.html")
render the fileindex.html
.settings
Settings that will be passed directly to_.templates
.cache
(default:true
) Iftrue
, templates will be cached, so changes to templates may require a restart. Iffalse
, templates will be recompiled every time a change occurs.globals
If given, the properties of this object will be available to all templates.ignoreState
(default:false
) Iftrue
, then properties ofctx.state
will not be available to templates.layout
Set the layout template. Thebody
variable is available in the layouts.
ctx.render( templateName, [ data, [ options ] ] )
Render a template. Works asynchronously, so you must handle the returned promise.
If ctx.type
has not been set, it will be automatically set to html
.
Takes the following options:
layout
(optional) If set, overrides the middlewarelayout
option.