View Template Rendering
In most cases, we need to fetch data and render with template files. So we need to use corresponding view engines.
egg-view is a built-in plugin to support using multiple view engines in one application. All view engines are imported as plugins. With egg-view developers can use the same API interface to work with different view engines. See View Plugin for more details.
Take the officially supported View plugin egg-view-nunjucks as an example:
# Installing View Engine Plugin
$ npm i egg-view-nunjucks --save |
# Enabling View Engine Plugin
// config/plugin.js |
# Configuring View Plugins
egg-view defines the default configuration of config.view
# root {String}
Root directory for template files is absolute path, with default value ${baseDir}/app/view
.
egg-view supports having multiple directories, which are separated by ,
.
In this case, it looks for template files from all the directories.
The configuration below is an example of multiple view directories:
// config/config.default.js |
# cache {Boolean}
Cache template file paths, default value is true
.
egg-view looks for template files from the directories that defined in root
.
When a file matching given template path is found, the file's full path will be cached
and reused afterward.
egg-view won't search all directories again for the same template path.
# mapping
and defaultViewEngine
Every view engine has a view engine name defined when the plugin is enabled.
In view configuration, mapping
defines the mapping
from template file's extension name to view engine name.
For example, use Nunjucks engine to render .nj
files.
module.exports = { |
egg-view uses the corresponding view engine according to the configuration above.
await ctx.render('home.nj'); |
The mapping from file extension name to view engine must be defined.
Otherwise egg-view cannot find correct view engine.
Global configuration can be done with defaultViewEngine
.
// config/config.default.js |
If a view engine cannot be found according to specified mapping, the default view engine will be used. For the applications that use only one view engine, it's recommended to set this option.
# defaultExtension
When calling render()
, the first argument should contain file extension name,
unless defaultExtension
has been configured.
// config/config.default.js |
# Rendering Page
egg-view provides three interfaces in Context. All three returns a Promise:
render(name, locals)
renders template file, and set the value toctx.body
.renderView(name, locals)
renders template file, returns the result and don't set the value to any variable.renderString(tpl, locals)
renders template string, returns the result and don't set the value to any variable.
// {app_root}/app/controller/home.js |
When calling renderString
, view engine should be specified unless defaultViewEngine
has been defined.
# locals
In the process of rendering pages, we usually need a variable to contain all information that is used in view template. egg-view provides app.locals
and ctx.locals
.
app.locals
is global, usually configured inapp.js
.ctx.locals
is per-request, and it mergesapp.locals
.ctx.locals
can be assigned by modifying key/value or assigned with a new object. egg-view will merge the new object automatically in corresponding setter.
// `app.locals` merged into `ctx.locals` |
In practical development, we usually don't use these two objects in controller directly.
Instead, simply call ctx.render(name, data)
:
- egg-view merges
data
intoctx.locals
automatically. - egg-view injects
ctx
,request
,helper
into locals automatically.
ctx.app.locals = { appName: 'showcase' }; |
Note:
- ctx.locals is cached. app.locals is merged into it only at the first time that ctx.locals is accessed.
- due to the ambiguity of naming, the
ctx.state
that is used in Koa is replaced byctx.locals
in Egg.js, i.e.ctx.state
andctx.locals
are equivalent. It's recommended to use the latter.
# Helper
All functions that defined in helper
can be directly used in templates.
See Extend for more details.
// app/extend/helper.js |
# Security
The built-in plugin egg-security provides common security helper functions, including helper.shtml / surl / sjs
and so on. It's strongly recommended to read Security.