The `` template literal mode '' of String in art-template template engine JS is downloaded through npm
art格式模板可以填写html一样的内容
引入atr-template模块
const template = require("art-template");
const path = require("path");
//将特定模板与特定数据进行拼接 数据路径使用绝对路径
const artpath = path.join(__dirname, "a", "index.art");
const html = template(artpath, {
data: {
name: "张三",
age: 20,
},
});
HTML use template
<div>
<span>{{data.name}}</span> //张三
<span>{{data.age}}</span> //20
</div>
Standard grammar and original grammar
{{数据}} <%=数据%>
Data can be easily calculated and ternary
{{a+b}} {{a?b:c}}
{{@数据}} 原文输出,可以输出HTML标签 <%-数据%>
{{@<div>123</div>}}
Conditional judgment
{{if 条件}}内容数据{{/if}}
{{if 条件1}}内容{{else if}}内容{{/if}}
<% if (条件) { %> 内容 <% } %>
<% if (条件1) { %> 内容 <% } else if (条件2) {%> 内容 <% } %>
cycle
{{each 数据}}{{/each}}
<% for () { %> 内容 <% } %>
<ul>
{{each data}}
<li>
{{$value.name}}
{{$value.age}}
</li>
</ul>
<% for (var i = 0 ; i < data.length;i++) { %>
<li>
<%= data[i].name %>
<%= data[i].age %>
</li>
<% } %>
Sub-templates usually use art files
{{include "模板路径"}}
<%include("模板路径")%>
Template inheritance
HTML {{block "自定义名字"}}{{/block}} 需要有对应
art {{extend "需要继承模板路径"}}
{{block "自定义名字"}} HTML内容{{/block}} 填充继承模板内对应的骨架
Template configuration
template.defaults.imports.变量名= 变量值;这种方式导入第三方模块
设置模板根目录 template.defaluts.root = 目录;
配置模板默认后缀 template.defaluts.extname = ".html";
dateformat date processing module