art-template 是一个性能出众模板引擎,无论在 NodeJS 还是在浏览器中都可以运行。
- 针对 V8 引擎优化,渲染速度出众
- 支持编译、运行时调试,可定位语法、渲染错误的模板语句
- 支持 NodeJS 与 浏览器。支持 Express、Koa、Webpack、RequireJS
- 支持模板包含与模板继承
- 兼容 EJS、Underscore、LoDash 模板语法
- 支持 ES 严格模式环境运行
- 同时支持原生 JavaScript 语法、简约语法
- 支持定义模板的语法规则
- 浏览器版本仅 5KB 大小
npm install art-template --save
<% if (user) { %>
<h2><%= user.name %></h2>
<% } %>
或:
{{if user}}
<h2>{{user.name}}</h2>
{{/if}}
var template = require('art-template');
var html = template(__diranme + '/tpl-user.art', {
user: {
name: 'aui'
}
});
框架支持:
- Express: express-art-template
- Koa: koa-art-template
var render = require('./tpl-user.art');
var html = render({
user: {
name: 'aui'
}
});
使用浏览器版本:lib/template-web.js
<script id="tpl-user" type="text/html">
<% if (user) { %>
<h2><%= user.name %></h2>
<% } %>
</script>
<script src="art-template/lib/template-web.js"></script>
<script>
var html = template('tpl-user', {
user: {
name: 'aui'
}
});
</script>
lib/template-web.js 支持 RequireJS 加载
// 基于模板名渲染模板
template(filename, data);
// 将模板源代码编译成函数
template.compile(source, options);
// 将模板源代码编译成函数并立刻执行
template.render(source, data, options);
art-template 同时支持 {{expression}}
简约语法与任意 JavaScript 表达式 <% expression %>
。
1. 标准输出
{{value}}
{{a ? b : c}}
{{a || b}}
{{a + b}}
or
<%= value %>
<%= a ? b : c %>
<%= a || b %>
<%= a + b %>
特殊变量可以使用下标方式访问:
{{$data['user list']}}
2. 原始输出
{{@value}}
or
<%- value %>
原始输出语句不会对 HTML
内容进行转义
{{if value}} ... {{/if}}
{{if v1}} ... {{else if v2}} ... {{/if}}
or
<% if (value) { %> ... <% } %>
<% if (value) { %> ... <% } else { %> ... <% } %>
{{each target}}
{{$index}} {{$value}}
{{/each}}
or
<% for(var i = 0; i < target.length; i++){ %>
<%= i %> <%= target[i] %>
<% } %>
target
支持Array
与Object
的迭代,其默认值为$data
$value
与$index
可以自定义:{{each target val key}}
{{set temp = data.sub.content}}
or
<% var temp = data.sub.content; %>
{{include './header.art'}}
{{include './header.art' data}}
or
<% include('./header.art') %>
<% include('./header.art', data) %>
include
第二个参数默认值为 $data
。
{{extend './layout.art'}}
{{block 'head'}} ... {{/block}}
模板继承允许你构建一个包含你站点共同元素的基本模板“骨架”。
layout.art:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>{{block 'title'}}My Site{{/block}}</title>
{{block 'head'}}
<link rel="stylesheet" href="main.css">
{{/block}}
</head>
<body>
{{block 'content'}}{{/block}}
</body>
</html>
index.art:
{{extend './layout.art'}}
{{block 'title'}}My Page{{/block}}
{{block 'head'}}
{{@parent}}
<link rel="stylesheet" href="custom.css">
{{/block}}
{{block 'content'}}
<p>This is just an awesome page.</p>
{{/block}}
渲染 index.art 后,将自动应用布局骨架。
<% print(val, val2, val3) %>
// 向模板中导入过滤器
template.defaults.imports.$dateFormat = function(date, format){/*[code..]*/};
template.defaults.imports.$timestamp = function(value){return value * 1000};
{{date | $timestamp | $dateFormat 'yyyy-MM-dd hh:mm:ss'}}
or
<%= $dateFormat($timestamp(date), 'yyyy-MM-dd hh:mm:ss') %>
$data
传入模板的数据{Object|array}
$imports
外部导入的所有变量,等同template.defaults.imports
{Object}
$options
模板编译选项{Object}
print
字符串输出函数{function}
include
子模板载入函数{function}
extend
布局模板导入函数{function}
block
模板块声明函数{function}
template.defaults.imports.$console = console;
<% $console.log('hello world') %>
模板外部所有的变量都需要使用 template.defaults.imports
注入、并且要在模板编译之前进行声明才能使用。
缓存默认是开启的,开发环境中可以关闭它:
template.defaults.cache = false;
从一个简单的例子说起,让模板引擎支持 ES6 ${name}
模板字符串的解析:
template.defaults.rules.push({
test: /\${([\w\W]*?)}/,
use: function(match, code) {
return {
code: code,
output: 'escape'
}
}
});
其中 test
是匹配字符串正则,use
是匹配后的调用函数。关于 use
函数:
- 参数:一个参数为匹配到的字符串,其余的参数依次接收
test
正则的分组匹配内容 - 返回值:必须返回一个对象,包含
code
与output
两个字段:code
转换后的 JavaScript 语句output
描述code
的类型,可选值:'escape'
编码后进行输出'raw'
输出原始内容false
不输出任何内容
加载 .art
模板:
var template = require('art-template');
var view = require('./index.art');
var html = view(data);
加载 .ejs
模板:
var template = require('art-template');
require.extensions['.ejs'] = template.extension;
var view = require('./index.ejs');
var html = view(data);
根据模板名渲染模板。
var html = template('/welcome.art', {
value: 'aui'
});
在浏览器中,
filename
请传入存放模板的元素id
编译模板并缓存。
// compile && cache
template('/welcome.art', 'hi, <%=value%>.');
// use
template('/welcome.art', {
value: 'aui'
});
编译模板并返回一个渲染函数。
var render = template.compile('hi, <%=value%>.');
var html = render({value: 'aui'});
编译并返回渲染结果。
var html = template.render('hi, <%=value%>.', {value: 'aui'});
模板引擎默认配置。参考 选项。
template.defaults
{
// 模板名字
filename: null,
// 模板语法规则列表
rules: [nativeRule, artRule],
// 是否支持对模板输出语句进行编码。为 false 则关闭编码输出功能
escape: true,
// 是否开启调试模式。如果为 true: {bail:false, cache:false, compileDebug:true}
debug: false,
// 是否开启缓存
cache: true,
// 是否编译调试版。编译为调试版本可以在运行时进行 DEBUG
compileDebug: false,
// 是否容错。如果为 true,编译错误与运行时错误都会抛出异常
bail: false,
// 模板路径转换器
resolveFilename: resolveFilename,
// HTML 压缩器
compressor: null,
// 错误调试器
debuger: debuger,
// 模板文件加载器
loader: loader,
// 缓存中心适配器(依赖 filename 字段)
caches: caches,
// 模板根目录。如果 filename 为全局路径,则会基于此查找模板
root: '/',
// 默认后缀名。如果没有后缀名,则会自动基于此补全
extname: '.art',
// 导入的模板变量
imports: {
$each: each,
$escape: escape,
$include: include
}
};