模板引擎art-template
模板引擎
官网
基础用法
# 将数据输出在模板中 # 标准语法{{数据}}# 原文输出 如果数据中携带 HTML元素标签,默认模板引擎不会解析标签,会将起转义输出 {{@数据}}# 原始语法<%= 数据 %># 原文输出<%- 数据 %>
》》可以是js表达式 是表达式 不是语句
》》条件判断
》》循环
模板配置
const template = require('art-template')
const moment = require('moment')
const path = require('path')
// 设置模板根目录
template.defaults.root=path.join(__dirname,'Views')
//设置模板默认后缀
template.defaults.extname = '.art'
//向模板中导入变量
template.defaults.imports.dayFormat=moment
let html = template('index',{username:'kaizen',date: new Date(),content :'<h1>xxx</h1>'
})
console.log(html)
过滤器
const template = require('art-template')
// 过滤器函数第一个参数接受目标值。
template.defaults.imports.dateFormat = function(date, format){/*[code..]*/};
template.defaults.imports.timestamp = function(value){return value * 1000};
模板继承
子模版
template template.compile template.render
// 基于模板名渲染模板
template(filename, data);// 将模板源代码编译成函数
template.compile(source, options);// 将模板源代码编译成函数并立刻执行
template.render(source, data, options);