JavaScript模板引擎Template.js使用详解
template.js 一款 JavaScript 模板引擎,简单,好用。提供一套模板语法,用户可以写一个模板区块,每次根据传入的数据,生成对应数据产生的HTML片段,渲染不同的效果。https://github.com/aui/artTemplate
1、特性
(1)、性能卓越,执行速度通常是 Mustache 与 tmpl 的 20 多倍(性能测试)(2)、支持运行时调试,可精确定位异常模板所在语句(演示)
(3)、对 NodeJS Express 友好支持(4)、安全,默认对输出进行转义、在沙箱中运行编译后的代码(Node版本可以安全执行用户上传的模板)
(5)、支持include语句
(6)、可在浏览器端实现按路径加载模板(详情)
(7)、支持预编译,可将模板转换成为非常精简的 js 文件
(8)、模板语句简洁,无需前缀引用数据,有简洁版本与原生语法版本可选
(9)、支持所有流行的浏览器
2、语法
(1)、使用
引用简洁语法的引擎版本,例如: <script src="dist/template.js"></script>
(2)、表达式
{{ 与 }} 符号包裹起来的语句则为模板的逻辑表达式。
(3)、输出表达式
对内容编码输出: {{content}}
不编码输出: {{#content}}
编码可以防止数据中含有 HTML 字符串,避免引起 XSS 攻击。
(4)、条件表达式
1
2
3
4
5
6
7
|
{{if admin}}
< p >admin</ p >
{{else if code > 0}}
< p >master</ p >
{{else}}
< p >error!</ p >
{{/if}}
|
(5)、遍历表达式
无论数组或者对象都可以用 each 进行遍历。
1
2
3
|
{{each list as value index}}
< li >{{index}} - {{value.user}}</ li >
{{/each}}
|
亦可以被简写:
1
2
3
|
{{each list}}
< li >{{$index}} - {{$value.user}}</ li >
{{/each}}
|
(6)、模板包含表达式
用于嵌入子模板。
{{include 'template_name'}}
子模板默认共享当前数据,亦可以指定数据:{{include 'template_name' news_list}}
(7)、辅助方法
使用template.helper(name, callback)注册公用辅助方法:
1
2
3
4
|
template.helper('dateFormat', function (date, format) {
// ..
return value;
});
|
模板中使用的方式: {{time | dateFormat:'yyyy-MM-dd hh:mm:ss'}}
支持传入参数与嵌套使用: {{time | say:'cd' | ubb | link}}
3、实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<!DOCTYPE HTML>
< html >
< head >
< meta charset = "UTF-8" >
< title >basic-demo</ title >
< script src = "../dist/template.js" ></ script >
</ head >
< body >
< div id = "content" ></ div >
< script id = "test" type = "text/html" >
{{if isAdmin}}
< h1 >{{title}}</ h1 >
< ul >
{{each list as value i}}
< li >索引 {{i + 1}} :{{value}}</ li >
{{/each}}
</ ul >
{{/if}}
</ script >
< script >
var data = {
title: '基本例子',
isAdmin: true,
list: ['文艺', '博客', '摄影', '电影', '民谣', '旅行', '吉他']
};
var html = template('test', data);
document.getElementById('content').innerHTML = html;
</ script >
</ body >
</ html >
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<!DOCTYPE HTML>
< html >
< head >
< meta charset = "UTF-8" >
< title >no escape-demo</ title >
< script src = "../dist/template.js" ></ script >
</ head >
< body >
< h1 >不转义HTML</ h1 >
< div id = "content" ></ div >
< script id = "test" type = "text/html" >
< p >不转义:{{#value}}</ p >
< p >默认转义: {{value}}</ p >
</ script >
< script >
var data = {
value: '< span style = "color:#F00" >hello world!</ span >'
};
var html = template('test', data);
document.getElementById('content').innerHTML = html;
</ script >
</ body >
</ html >
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<!DOCTYPE HTML>
< html >
< head >
< meta charset = "UTF-8" >
< title >include-demo</ title >
< script src = "../dist/template.js" ></ script >
</ head >
< body >
< div id = "content" ></ div >
< script id = "test" type = "text/html" >
< h1 >{{title}}</ h1 >
{{include 'list'}}
</ script >
< script id = "list" type = "text/html" >
< ul >
{{each list as value i}}
< li >索引 {{i + 1}} :{{value}}</ li >
{{/each}}
</ ul >
</ script >
< script >
var data = {
title: '嵌入子模板',
list: ['文艺', '博客', '摄影', '电影', '民谣', '旅行', '吉他']
};
var html = template('test', data);
document.getElementById('content').innerHTML = html;
</ script >
</ body >
</ html >
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
<!DOCTYPE HTML>
< html >
< head >
< meta charset = "UTF-8" >
< title >helper-demo</ title >
< script src = "../dist/template.js" ></ script >
</ head >
< body >
< h1 >辅助方法</ h1 >
< div id = "content" ></ div >
< script id = "test" type = "text/html" >
{{time | dateFormat:'yyyy年 MM月 dd日 hh:mm:ss'}}
</ script >
< script >
/**
* 对日期进行格式化,
* @param date 要格式化的日期
* @param format 进行格式化的模式字符串
* 支持的模式字母有:
* y:年,
* M:年中的月份(1-12),
* d:月份中的天(1-31),
* h:小时(0-23),
* m:分(0-59),
* s:秒(0-59),
* S:毫秒(0-999),
* q:季度(1-4)
* @return String
* @author yanis.wang
* @see http://yaniswang.com/frontend/2013/02/16/dateformat-performance/
*/
template.helper('dateFormat', function (date, format) {
if (typeof date === "string") {
var mts = date.match(/(\/Date(\d+)\/)/);
if (mts && mts.length >= 3) {
date = parseInt(mts[2]);
}
}
date = new Date(date);
if (!date || date.toUTCString() == "Invalid Date") {
return "";
}
var map = {
"M": date.getMonth() + 1, //月份
"d": date.getDate(), //日
"h": date.getHours(), //小时
"m": date.getMinutes(), //分
"s": date.getSeconds(), //秒
"q": Math.floor((date.getMonth() + 3) / 3), //季度
"S": date.getMilliseconds() //毫秒
};
format = format.replace(/([yMdhmsqS])+/g, function(all, t){
var v = map[t];
if(v !== undefined){
if(all.length > 1){
v = '0' + v;
v = v.substr(v.length-2);
}
return v;
}
else if(t === 'y'){
return (date.getFullYear() + '').substr(4 - all.length);
}
return all;
});
return format;
});
// --------
var data = {
time: 1408536771253,
};
var html = template('test', data);
document.getElementById('content').innerHTML = html;
</ script >
</ body >
</ html >
|
以上就是本文的全部内容,希望对大家的学习有所帮助
JavaScript模板引擎Template.js使用详解相关推荐
- js模板引擎Template.js使用详解及免费下载template.js
这篇文章主要为大家详细介绍了JavaScript模板引擎Template.js使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 template.js 一款 JavaScript 模板引擎, ...
- JavaScript模板引擎Template.js使用
文章主要为大家详细介绍了JavaScript模板引擎Template.js使用方法.template.js 一款 JavaScript 模板引擎,简单,好用.提供一套模板语法,用户可以写一个模板区块, ...
- JavaScript模板引擎Template.js基本使用详解
template.js是一款JavaScript模板引擎,提供一套模板语法,简单好用,开发者可以写一个模板区块,每次传入的数据,生成对应数据产生的HTML片段,渲染不同的效果.官网:简洁语法版 htt ...
- JavaScript模板引擎Template.js
template.js 一款 JavaScript 模板引擎,简单,好用.提供一套模板语法,用户可以写一个模板区块,每次根据传入的数据,生成对应数据产生的HTML片段,渲染不同的效果.https:// ...
- 高性能JavaScript模板引擎template.js原理解析
点击有惊喜 artTemplate是新一代javascript模板引擎,它在v8中的渲染效率可接近javascript性能极限,在chrome下渲染效率测试中分别是知名引擎Mustache与micro ...
- Javascript模板引擎mustache.js详解
进阶之路在于日积月累, 每天进步一点点-加油~ mustache.js是一个简单强大的Javascript模板引擎,使用它可以简化在js代码中的html编写,压缩后只有9KB,非常值得在项目中使用. ...
- 模板引擎(art-template)详解
art-template
- JavaScript模板引擎详解
JavaScript模板引擎是一种用于生成HTML.XML和其他文本格式的工具,它将特定的模板语法转换为相应的文本输出,通常用于前端开发中动态生成页面内容.下面是一个JavaScript模板引擎的详细 ...
- Three.js实例详解___旋转的精灵女孩(附完整代码和资源)(一)
Three.js实例详解___旋转的精灵女孩(附完整代码和资源)(一) 本文目录: 一.[旋转的精灵女孩]案例运行效果 二.Three.js简介 三.Three.js代码正常运行显示条件 (1)不载入 ...
最新文章
- Error:Execution failed for task ':app:mergeDebugResources'. Error: java.lang.RuntimeException: Som
- Java——BigInteger类和BigDecimal类
- 那些拧不开瓶盖的女生全都是装的?理工男这样想......
- Context mContext = getApplicationContext();
- Nginx gzip参数详解及常见问题(已解决)
- KeyShot实例教程教你怎么模拟水面效果
- Spring cloud--鸿鹄Cloud分布式微服务云系统—Config
- java获取世界各国地址,获取世界各国、全国省份、城市、县
- python代码美化工具black
- openGL ES进阶教程(六)美颜滤镜之美白,磨皮,红润
- 阿里云硬盘挂载与扩容
- Java绘制太极阴阳图
- Java SE核心I
- 微型计算机系统与接口重要吗,微型计算机系统与接口 马宏锋 等编著 第六章
- Android App实战项目之实现手写签名APP功能(附源码,简单易懂 可直接实用)
- 如何让Js代码只执行一次
- 电机三角形接法和星形接法的理解
- crc16校验c语言程序源码,CRC16代码(C语言实现)
- win98老机子安装linux,Windows 98 安装全过程(转)
- python按照队列顺序打印文件
热门文章
- 往excel新增sheet出错原因
- ai预测占比_2020年AI手机占比将达80% 10种新功能应运而生
- (IOS可自动播放)使用bxslider做了一个切换图片跟随播放MP3的功能
- 计算机在小学语文教学中的应用的现状,多媒体在小学低年级语文教学中的应用...
- 《JavaScript语言精粹 修订版》 读书笔记
- 彻底讲透xposed原理
- matlab 音调_音调策略的问题
- 把时间戳转换成倒计时
- 【RPA】 RPA技术的核心概念及其应用范围——如何让公司的核心业务流程自动化,以及如何把RPA真正落地到企业中?
- python知识点全复习--跟随老师的ppt的代码进行复习