1.0.2 • Published 6 months ago
art-compiler v1.0.2
art-compiler
从 art-template 精简而来,删除了模板嵌套,仅保留了解析功能
// file: source.art
// 设置
{{set temp = data.sub.content}}
// 变量
{{temp}}
{{value}}
{{data.key}}
{{data['key']}}
{{a ? b : c}}
{{a || b}}
{{a + b}}
{{$data['user list']}}
// 条件
{{if value}} ... {{/if}}
{{if v1}} ... {{else if v2}} ... {{/if}}
// 循环
{{loop target}}
{{$index}} {{$value}}
{{/loop}}
// 使用 glb 过滤函数
{{date | timestamp | dateFormat 'yyyy-MM-dd hh:mm:ss'}}
// 从 glb 函数获取数据
{{set user = await $glb.getUser()}}
{{user.name}}
使用
const { artBuild } = require('art-compiler');
// 编译 source content
const compiler = artBuild(source, 'filename')
// 直接运行, 接受两个参数 data/glb
// data: 模板所需变量
// glb: 定义可用的函数
const txt = await compiler.render({
key
}, {
timestamp(data){
return data;
},
async getUser() {
return {name:''}
}
})
// 也保存解析结果, 下次直接使用
saveFileTo(savepath, compiler.code)
const render = require(savepath)
const text = await render(data, glb)