1-1 安装依赖
npm i egg-view-nunjucks --save
1-2 config/plugin.js
module.exports = {
nunjucks: {
enable: true,
package: 'egg-view-nunjucks',
}
};
1-3 config/config.default.js
config.view = {
defaultViewEngine: 'nunjucks',
mapping: {
'.html': 'nunjucks',
},
};
1-4 app/view
// 添加 view 配置
1-5 使用
controller home.js
class HomeController extends Controller {
async index() {
const { ctx } = this;
await ctx.render("home",{arr:["html","css","js"],isShow:false})
}
}
view home.html
{% for item in arr %}
<p>{{item}}</p>
{% endfor %}
{% if isShow%}
<p>show</p>
{% endif %}