Package detail

egg-view-ejs

eggjs6kMIT3.0.0

egg view plugin for ejs

egg, eggPlugin, egg-plugin, egg-view

readme

egg-view-ejs

NPM version NPM quality NPM download

Continuous Integration Test coverage

egg view plugin for ejs.

Install

$ npm i egg-view-ejs --save

Usage

// {app_root}/config/plugin.js
exports.ejs = {
  enable: true,
  package: 'egg-view-ejs',
};

// {app_root}/config/config.default.js
exports.view = {
  mapping: {
    '.ejs': 'ejs',
  },
};

Create a ejs file

// app/view/hello.ejs
hello <%= data %>

Render it

// app/controller/render.js
exports.ejs = async ctx => {
  await ctx.render('hello.ejs', {
    data: 'world',
  });
};

The file will be compiled and cached, you can change config.ejs.cache = false to disable cache, it's disable in local env by default.

Include

You can include both relative and absolute file.

Relative file is resolve from current file path.

// app/view/a.ejs include app/view/b.ejs
<% include('b.ejs') %>

Absolute file is resolve from app/view.

// app/view/home.ejs include app/view/partial/menu.ejs
<% include('/partial/menu.ejs') %>

Layout

You can render a view with layout also:

// app/view/layout.ejs

<% body %>

// app/controller/render.js
exports.ejs = async ctx => {
  const locals = {
    data: 'world',
  };

  const viewOptions = {
    layout: 'layout.ejs'
  };

  await ctx.render('hello.ejs', locals, viewOptions);
};

Configuration

see config/config.default.js for more detail.

Questions & Suggestions

Please open an issue here.

License

MIT

changelog

3.0.0 / 2022-06-29

others

2.0.1 / 2020-02-03

others

2.0.0 / 2017-11-15

others

1.1.0 / 2017-03-27

  • feat: add layout (#6)
  • docs: can't overrite default view plugin (#5)

1.0.1 / 2017-02-23

  • fix: missing app.js in pkg.files

1.0.0 / 2017-02-03

  • init version