eggjs/egg-view

Name: egg-view

Owner: egg

Description: null

Created: 2017-02-18 16:17:22.0

Updated: 2018-05-14 07:46:50.0

Pushed: 2018-03-14 09:40:23.0

Homepage: null

Size: 32

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

egg-view

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

Base view plugin for egg

it's a plugin that has been built-in for egg.

Install
m i egg-view --save
Usage
app_root}/config/plugin.js
rts.view = {
able: true,
ckage: 'egg-view',

Use a template engine

egg-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.

onfig/plugin.js
rts.ejs = {
able: true,
ckage: 'egg-view-ejs',

Configure the mapping, the file with .ejs extension will be rendered by ejs.

onfig/config.default.js
rts.view = {
pping: {
'.ejs': 'ejs',


In controller, you can call ctx.render.

le.exports = app => {
turn 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.

le.exports = app => {
turn class UserController extends app.Controller {
async list() {
  const { ctx } = this;
  ctx.body = await ctx.renderString('<%= user %>', { user: 'popomore' }, {
    viewEngine: 'ejs',
  });
}


Use multiple view engine

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

onfig/config.default.js
rts.view = {
pping: {
'.ejs': 'ejs',
'.nj': 'nunjucks',


You can simply render the file with .nj extension.

t ctx.render('user.nj');
How to write a view plugin

You can use egg-view' API to register a plugin.

View engine

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.

ib/view.js
le.exports = class MyView {
nstructor(ctx) {
// do some initialize
// get the plugin config from `ctx.app.config`


ync render(fullpath, locals) {
return myengine.render(fullpath, locals);


ync 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.

s MyView {
nder(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)

renderString(tpl, locals, viewOptions)

Register

After define a view engine, you can register it.

pp.js
le.exports = app => {
p.view.use('myName', require('./lib/view'));

You can define a view engine name, normally it's a template name.

Configure

Define plugin name and depend on egg-view


ggPlugin": {
"name": "myName",
"dependencies": [ "view" ]


Set default config in config/config.default.js, the name is equals to plugin name.

rts.myName = {},

See some examples

Configuration
Root

Root is ${baseDir}/app/view by default, but you can define multiple directory, seperated by ,. egg-view will find a file from all root directories.

le.exports = appInfo => {
nst baseDir = appInfo.baseDir;
turn {
view: {
  root: `${baseDir}/app/view,${baseDir}/app/view2`
}


defaultExtension

When render a file, you should specify a extension that let egg-view know whitch engine you want to use. However you can define defaultExtension without write the extension.

onfig/config.default.js
rts.view = {
faultExtension: '.html',


ontroller
le.exports = app => {
turn class UserController extends app.Controller {
async list() {
  const { ctx } = this;
  // render user.html
  await ctx.render('user');
}


viewEngine and defaultViewEngine

If you are using renderString, you should specify viewEngine in view config, see example above.

However, you can define defaultViewEngine without set each time.

onfig/config.default.js
rts.view = {
faultViewEngine: 'ejs',

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

Questions & Suggestions

Please open an issue here.

License

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.