1、安装模版
cnpm i egg-view-nunjucks -S
2、配置config/plugin.js
module.exports = {
// had enabled by egg
nunjucks : {
enable: true,
package: 'egg-view-nunjucks'
}
};
3、配置config/config.default.js
// start 配置art
config.view = {
defaultViewEngine: 'nunjucks',
mapping: {
'.html': 'nunjucks',
},
};
// end atr
4、在app下新建一个view的文件夹
在view文件夹里放置.html文件
5、在controller中使用render
const Controller = require('egg').Controller;
class HomeController extends Controller {
async index() {
const { ctx } = this;
await ctx.render("home")
}
}
module.exports = HomeController;
6、模版语法
for:
{% for item in Array %}
<p>{{item}}</p>
{% endfor %}
if:
{% if Boolean %}
<p>show</p>
{% endif %}