kne-egg-view-ejs

0.0.4 • Public • Published

kne-egg-view-ejs

NPM version build status Test coverage David deps Known Vulnerabilities npm download

egg view plugin for ejs.

English | 简体中文

Install

$ npm i kne-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',
  },
};

// ejs config
exports.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);
};

use callback

// app/view/layout.ejs

<%- body%>

// app/controller/render.js
exports.ejs = async ctx => {

  await ctx.render('config.ejs', argss, {
          layout: 'layout.ejs',
          fn: (html) => {
            return jwt.sign(JSON.parse('{' + html + '}'), cfgSignatureSecret, { expiresIn: cfgSignatureSecretExpiresIn });
          }
        });
};

Configuration

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

Questions & Suggestions

Please open an issue here.

License

MIT

Package Sidebar

Install

npm i kne-egg-view-ejs

Weekly Downloads

1

Version

0.0.4

License

MIT

Unpacked Size

11.5 kB

Total Files

8

Last publish

Collaborators

  • shonn