plugins/view/README.md
Base view plugin for egg
it's a plugin that has been built-in for egg.
npm i @eggjs/view
// {app_root}/config/plugin.ts
export default {
view: {
enable: true,
package: '@eggjs/view',
},
};
@eggjs/view don't have build-in view engine, So you should choose a template engine like ejs, and install egg-view-ejs plugin.
You can choose a template engine first, link ejs, so we use egg-view-ejs plugin.
egg-view is in eggjs, so you just need configure egg-view-ejs.
// config/plugin.ts
export default {
ejs: {
enable: true,
package: 'egg-view-ejs',
},
};
Configure the mapping, the file with .ejs extension will be rendered by ejs.
// config/config.default.ts
export default {
view: {
mapping: {
'.ejs': 'ejs',
},
},
};
In controller, you can call ctx.render.
export default (app: Application) => {
return class UserController extends app.Controller {
async list() {
const { ctx } = this;
await ctx.render('user.ejs');
}
};
}
If you call ctx.renderString, you should specify viewEngine in viewOptions.
export default (app: Application) => {
return class UserController extends app.Controller {
async list() {
const { ctx } = this;
ctx.body = await ctx.renderString(
'<%= user %>',
{ user: 'popomore' },
{
viewEngine: 'ejs',
}
);
}
};
}
[egg-view] support multiple view engine, so you can use more than one template engine in one application.
If you want add another template engine like nunjucks, then you can add egg-view-nunjucks plugin.
Configure the plugin and mapping
// config/plugin.ts
export default {
ejs: {
enable: true,
package: 'egg-view-ejs',
},
nunjucks: {
enable: true,
package: 'egg-view-nunjucks',
},
};
// config/config.default.ts
export default {
view: {
mapping: {
'.ejs': 'ejs',
'.nj': 'nunjucks',
},
},
};
You can simply render the file with .nj extension.
await ctx.render('user.nj');
You can use @eggjs/view's API to register a plugin.
Create a view engine class first, and implement render and renderString, if the template engine don't support, just throw an error.
The view engine is context level, so it receive ctx in constructor.
// lib/view.ts
export default class MyView {
constructor(ctx) {
// do some initialize
// get the plugin config from `ctx.app.config`
}
async render(fullpath: string, locals: Record<string, any>) {
return myengine.render(fullpath, locals);
}
async renderString() {
throw new Error('not implement');
}
}
render and renderString support generator function, async function, or normal function return a promise.
If the template engine only support callback, you can wrap it by Promise.
class MyView {
render(fullpath, locals) {
return new Promise((resolve, reject) => {
myengine.render(fullpath, locals, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
}
These methods receive three arguments, renderString will pass tpl as the first argument instead of name in render.
render(name, locals, viewOptions)
app/view by default)config/config.default.js. Plugin should implement it if it has config.
When you implement view engine, you will receive this options from render, the options contain:
renderString(tpl, locals, viewOptions)
renderStringrenderrenderAfter define a view engine, you can register it.
// app.ts
import type { Application } from 'egg';
import MyView from './lib/view';
export default (app: Application) => {
app.view.use('myName', MyView);
}
You can define a view engine name, normally it's a template name.
Define plugin name and depend on @eggjs/view
{
"eggPlugin": {
"name": "myName",
"dependencies": ["view"]
}
}
Set default config in config/config.default.ts, the name is equals to plugin name.
export default {
myName: {},
};
See some examples
Root is ${baseDir}/app/view by default, but you can define multiple directory, seperated by ,.
@eggjs/view will find a file from all root directories.
export default (appInfo: EggAppInfo) => {
const baseDir = appInfo.baseDir;
return {
view: {
root: `${baseDir}/app/view,${baseDir}/app/view2`,
},
};
}
When render a file, you should specify a extension that let @eggjs/view know whitch engine you want to use.
However you can define defaultExtension without write the extension.
// config/config.default.ts
export default {
view: {
defaultExtension: '.html',
},
};
// controller
export default (app: Application) => {
return class UserController extends app.Controller {
async list() {
const { ctx } = this;
// render user.html
await ctx.render('user');
}
}
}
If you are using renderString, you should specify viewEngine in view config, see example above.
However, you can define defaultViewEngine without set each time.
// config/config.default.ts
export default {
view: {
defaultViewEngine: 'ejs',
},
};
see config/config.default.ts for more detail.
Please open an issue here.
Made with contributors-img.