Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

toajs/toa-ejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

toa-ejs

Ejs render module for toa.

NPM version Build Status Downloads

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 is layout, set false to disable layout.
  • options.viewExt: view file extension, default is html.
  • options.delimiter: Character to use with angle brackets for open/close, default is %.
  • options.cache: cache compiled function, default is true.
  • options.debug: Output generated function body.
  • options.compileDebug: When false no debug instrumentation is compiled, default is false.
  • options.context: Template function execution context, default is null.
  • options.locals: global locals, can be function type, this in the function is toa's context.
  • options.writeResp: Write template to response body, default is true.

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

return thunk function.

  • options.layout: global layout file, default is layout, set false to disable layout.
  • options.writeResp: Write template to response body, default is true.
  • options.debug: Output generated function body.
  • options.compileDebug: When false no debug instrumentation is compiled, default is false.
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)