Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@koa/ejs

koajs71kMIT5.1.0

ejs render middleware for Koa.js

koa, middleware, render, ejs, view, template-engine

readme

@koa/ejs

Koa ejs view render middleware. support all feature of ejs.

Build Status

NPM

Usage

Example

const Koa = require("koa");
const render = require("@koa/ejs");
const path = require("path");

const app = new Koa();
render(app, {
  root: path.join(__dirname, "view"),
  layout: "template",
  viewExt: "html",
  cache: false,
  debug: true,
});

app.use(async function (ctx) {
  await ctx.render("user");
});

app.listen(7001);

Or you can checkout the example.

Settings

  • root: view root directory.
  • fs: file system module with same Node.js fs interface (default Node.js fs module).
  • layout: global layout file, default is layout, set false to disable layout.
  • viewExt: view file extension (default html).
  • cache: cache compiled templates (default true).
  • debug: debug flag (default false).
  • delimiter: character to use with angle brackets for open / close (default %).
  • async: When true, EJS will use an async function for rendering. Depends on async/await support in the JS runtime.
  • outputFunctionName: Set to a string (e.g., 'echo' or 'print') for a function to print output inside scriptlet tags.

Layouts

@koa/ejs supports layouts. The 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 await ctx.render. Also you can set layout = false to disable the layout.

<html>
  <head>
    <title>koa ejs</title>
  </head>
  <body>
    <h3>koa ejs</h3>
    <%- body %>
  </body>
</html>

Include

Supports ejs includes.

<div>
  <%- include ('user.html') -%>
</div>

State

Support ctx.state in koa.

TypeScript

If you're project based on TypeScript, we recommend using @types/koa-ejs until we start supporting it in the upcoming releases.

Licences

(The MIT License)

changelog

History

PLEASE REFERENCE THE RELEASES TAB FOR THE LATEST CHANGES

4.3.0 / 2019-12-04

features

4.2.1 / 2019-12-04

others

4.2.0 / 2018-12-27

features

4.1.2 / 2018-07-19

fixes

4.1.1 / 2018-03-17

fixes

  • [f12513f] - fix: Fix Wrong function reference may cause 'Maxiumn call stack size exceeded' (Runrioter <runrioter@qq.com>)

others

4.1.0 / 2017-06-27

  • feat: Make viewExt option support include directive (#38)
  • correct the demo in the Readme

4.0.1 / 2017-06-25

  • Fix debug setting bug (#37)

4.0.0 / 2017-02-28

  • rewrite with async function and upgrade dependencies (#34)

3.0.0 / 2015-11-19

  • Clean open/close settings
  • Switch to EJS v2

2.0.1 / 2015-04-22

  • fix: contenxt not defined. fixes #18

2.0.0 / 2015-04-19

  • add test for 0.12 and iojs
  • deps: ejs@1
  • refactor: remove locals

1.1.3 / 2014-12-18

  • fix viewPath' bug, improve code

1.1.2 / 2014-08-30

  • Merge pull request #8 from zedgu/master
  • for resolve to absolute path

1.1.1 / 2014-08-28

  • Merge pull request #7 from lopezchr/clopez
  • adding write-response test suit
  • Addign no auto write body option

1.1.0 / 2014-08-12

  • Merge pull request #6 from zedgu/master
  • [fix] open/close tag setting is not working

1.0.2 / 2014-07-14

  • Merge pull request #5 from its-florida/master
  • Honor layout overrides in options.

1.0.1 / 2014-05-04

  • updated default settings, fixes #3

1.0.0 / 2014-04-24

  • refactor, close #2

0.0.3 / 2014-03-17

  • update readme
  • support locals as generator
  • Merge pull request #1 from Pana/master
  • small edit

0.0.2 / 2014-03-11

  • fix charset
  • fix typo

0.0.1 / 2014-02-25

  • update readme
  • add test
  • rename koa-view to koa-ejs
  • rename koa-render to koa-view
  • add example
  • complete koa-render
  • Initial commit