egg view plugin for ejs
npm install egg-view-ejs




egg view plugin for [ejs].
``bash`
$ npm i egg-view-ejs --save
`js
// {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
`js`
// app/view/hello.ejs
hello <%= data %>
Render it
`js`
// 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.
You can include both relative and absolute file.
Relative file is resolve from current file path.
`html`
// app/view/a.ejs include app/view/b.ejs
<% include('b.ejs') %>
Absolute file is resolve from app/view.
`html`
// app/view/home.ejs include app/view/partial/menu.ejs
<% include('/partial/menu.ejs') %>
You can render a view with layout also:
`js
// 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);
};
``
see config/config.default.js for more detail.
Please open an issue here.
[ejs]: https://github.com/mde/ejs