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

  1. rootMultiple template directories can be specified
  2. Must be configuredmappingSpecify the environment for the engine and file
  3. cacheConfiguration items are only used to cache paths,Does not cache templates or template compilation results

Use of template engines

Egg provides three methods

  1. ctx.renderThis method is the only one that returns the result toctx.body
  2. ctx.renderView
  3. ctx.renderString

Principle of internal invocation

Take the ctx.renderView method as an example: