Awesome
toa-ejs
Ejs render module for toa.
toa
It is a Implementation of v2.x https://github.com/mde/ejs. Checkout v1.x for https://github.com/visionmedia/ejs
Example
const Toa = require('toa')
const toaEjs = require('toa-ejs')
const app = new Toa()
toaEjs(app, {
root: 'views',
layout: 'template',
viewExt: 'html',
cache: false,
locals: locals
})
app.use(function * () {
yield this.render('user', {name: 'toa', age: 1})
})
app.listen(3000)
Or you can checkout the example.
Installation
npm install toa-ejs
API
const toaEjs = require('toa-ejs')
toaEjs(app, options)
It will add render
method to context
.
options.root
: views root directory, required.options.layout
: global layout file, default islayout
, setfalse
to disable layout.options.viewExt
: view file extension, default ishtml
.options.delimiter
: Character to use with angle brackets for open/close, default is%
.options.cache
: cache compiled function, default istrue
.options.debug
: Output generated function body.options.compileDebug
: Whenfalse
no debug instrumentation is compiled, default isfalse
.options.context
: Template function execution context, default isnull
.options.locals
: global locals, can be function type,this
in the function is toa'scontext
.options.writeResp
: Write template to response body, default istrue
.
context.render(viewName, [data], [options])
return thunk function.
options.layout
: global layout file, default islayout
, setfalse
to disable layout.options.writeResp
: Write template to response body, default istrue
.options.debug
: Output generated function body.options.compileDebug
: Whenfalse
no debug instrumentation is compiled, default isfalse
.
this.render('user', {name: 'toa', age: 1})
this.render('user', {name: 'toa', age: 1}, {compileDebug: true})
Layouts
toa-ejs
support layout. default layout file is layout
, if you want to change default layout file, use settings.layout
. Also you can specify layout by options.layout
in this.render
.
Also you can set layout = false;
to close layout.
<html>
<head>
<title>toa ejs</title>
</head>
<body>
<h3>toa ejs</h3>
<%- body %>
</body>
</html>
Include
support ejs default include.
<div>
<%- include('user/show', {user: user}); %>
</div>
Locals
pass gobal locals by settings.locals
, locals can be functions that can be called in ejs templates.
const locals = {
version: 'v1.0.0',
now: function() {
return new Date()
},
__: function() {
return this.__.apply(this, arguments) // toa-i18n's `__` method.
}
}
License
The MIT License (MIT)