art-template:解决拼接字符串更加方便
【1】简单字符串应用
str = `
<ul>
<li>${student[index].name}</li>
<li>${student[index].sex}</li>
</ul>
`;
【2】与文件内容关联
const template = require('art-template');
const path = require('path');
const views = path.join(__dirname, 'views', '2');
const htmlStr = template(views, {
sname: 'macro',
content: '<span>243</span>'
});
console.log(htmlStr);
//与.art文件关联
<div>
<%=sname%>
</div>
<div>{{sname}}</div>
<p>{{2+4}}</p>
<p>{{10>3?4:5}}</p>
<p>{{content}}</p>
<p>{{@ content}}</p> // 解析<spans></span>
[3]逻辑判断条件
const htmlStr = template(views, {
age: 23,
students: [{
name: 'macr0',
age: 4,
sex: 'male'
}, {
name: 'macr0',
age: 24,
sex: 'male'
}, {
name: 'macr0',
age: 14,
sex: 'male'
}]
});
//
{{if age >= 16}}
可以考取大学
{{else}}
回高中读书
{{/if}}
<ul>
{{each students}}
<li>
{{$value.name}}
{{$value.age}}
{{$value.sex}}
</li>
{{/each}}
</ul>