The plug-in configuration item for the template engine in egg
config.view = {
// Multiple absolute paths You can configure multiple template directories
root: [
].join(', '),
// Whether to cache path lookup, if enabled, will cache through the root matched path, the next time will not find the path again
// TIP: Note that you are not caching the template or compiling the result of the template
cache: true.// You can configure which template engine to use for different file suffixes
mapping: {
'.nj': 'nunjucks'
},
// You can configure the default template engine so that when there is no match to the template engine through the suffix, the engine specified by this configuration item will be used for rendering
defaultViewEngine: ' '.// The default file suffix, if specified, can be used with the render method without the file suffix
defaultExtension: ' ',}Copy the code
Pay attention to the point
root
Multiple template directories can be specified- Must be configured
mapping
Specify the environment for the engine and file cache
Configuration items are only used to cache paths,Does not cache templates or template compilation results
Use of template engines
Egg provides three methods
ctx.render
This method is the only one that returns the result toctx.body
ctx.renderView
ctx.renderString
Principle of internal invocation
Take the ctx.renderView method as an example: