Home

Awesome

toa-ejs

Ejs render module for toa.

NPM version Build Status Downloads

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.

context.render(viewName, [data], [options])

return thunk function.

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)