1、安装
npm install --save art-template
npm install --save koa-art-template
2、引入
var render = require('koa-art-template');
3、配置(需要引入path)
render(app,{
root:path.join(_dirname,'views'), //视图位置
extname:'.html', //模板后缀名
debug:process.env.NODE_ENV !== 'production' //是否开启调试
})
4、渲染模板
await ctx.render('user');
5、绑定数据
语法一:大部分类似ejs
引入模板:
<% include('./public/footer.html') %>
语法二:类似于angular
router.get('/'.async (ctx) => {
let list = {
name:'slj',
h:'<h2>这是一个html</h2>',
num:20,
data:['111','222','333']
}
await ctx.render('news',{
list:list
})
})
在模板html中渲染数据的方式:
{{list.name}} //slj
{{@list.h}} //这是一个html
{{if num>20}}
<h3>num大于20</h3>
{{else}}
<h3>num小于20</h3>
{{/if}}
{{each list.data}}
<li>{{$index}} --- {{$value}}</li>
{{/each}}
//引入模板
{{include 'public/footer.html'}}