1-1 安装依赖

  1. npm i egg-view-nunjucks --save

1-2 config/plugin.js

  1. module.exports = {
  2. nunjucks: {
  3. enable: true,
  4. package: 'egg-view-nunjucks',
  5. }
  6. };

1-3 config/config.default.js


  1. config.view = {
  2. defaultViewEngine: 'nunjucks',
  3. mapping: {
  4. '.html': 'nunjucks',
  5. },
  6. };

1-4 app/view

// 添加 view 配置

1-5 使用

controller home.js

  1. class HomeController extends Controller {
  2. async index() {
  3. const { ctx } = this;
  4. await ctx.render("home",{arr:["html","css","js"],isShow:false})
  5. }
  6. }

view home.html

  1. {% for item in arr %}
  2. <p>{{item}}</p>
  3. {% endfor %}
  4. {% if isShow%}
  5. <p>show</p>
  6. {% endif %}