JavaScript中template模板引擎

template.js 一款 JavaScript 模板引擎,简单,好用。提供一套模板语法,用户可以写一个模板区块,每次根据传入的数据,生成对应数据产生的HTML片段,渲染不同的效果。

1、特性

(1)、性能卓越,执行速度通常是 Mustache 与 tmpl 的 20 多倍(性能测试)(2)、支持运行时调试,可精确定位异常模板所在语句(演示)

(3)、对 NodeJS Express 友好支持(4)、安全,默认对输出进行转义、在沙箱中运行编译后的代码(Node版本可以安全执行用户上传的模板)

(5)、支持include语句

(6)、可在浏览器端实现按路径加载模板(详情)

(7)、支持预编译,可将模板转换成为非常精简的 js 文件

(8)、模板语句简洁,无需前缀引用数据,有简洁版本与原生语法版本可选

(9)、支持所有流行的浏览器

2、语法

(1)、使用

   引用简洁语法的引擎版本,例如:

(2)、表达式

   {{ 与 }} 符号包裹起来的语句则为模板的逻辑表达式。

(3)、输出表达式

  对内容编码输出: {{content}}
  不编码输出: {{#content}}
   编码可以防止数据中含有 HTML 字符串,避免引起 XSS 攻击。

(4)、条件表达式

1 {{if admin}}
2  <p>admin</p>
3 {{else if code > 0}}
4  <p>master</p>
5 {{else}}
6  <p>error!</p>
7 {{/if}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

(5)、遍历表达式

无论数组或者对象都可以用 each 进行遍历。

1 {{each list as value index}}
2  <li>{{index}} - {{value.user}}</li>
3 {{/each}}
  • 1
  • 2
  • 3

亦可以被简写:

1 {{each list}}
2  <li>{{$index}} - {{$value.user}}</li>
3 {{/each}}
  • 1
  • 2
  • 3

(6)、模板包含表达式

用于嵌入子模板。

1 {{include ‘template_name’}}

子模板默认共享当前数据,亦可以指定数据:{{include ‘template_name’ news_list}}

(7)、辅助方法

使用template.helper(name, callback)注册公用辅助方法:

1 template.helper('dateFormat', function (date, format) {
2  // ..
3  return value;
4 });
  • 1
  • 2
  • 3
  • 4

模板中使用的方式: {{time | dateFormat:’yyyy-MM-dd hh:mm:ss’}}
支持传入参数与嵌套使用: {{time | say:’cd’ | ubb | link}}

3、实例

 1 <!DOCTYPE HTML>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>basic-demo</title>
 6 <script src="../dist/template.js"></script>
 7 </head>
 8 <body>
 9 <div id="content"></div>
10 <script id="test" type="text/html">
11 {{if isAdmin}}
12 <h1>{{title}}</h1>
13 <ul>
14  {{each list as value i}}
15   <li>索引 {{i + 1}} :{{value}}</li>
16  {{/each}}
17 </ul>
18 {{/if}}
19 </script>
20 <script>
21 var data = {
22  title: '基本例子',
23  isAdmin: true,
24  list: ['文艺', '博客', '摄影', '电影', '民谣', '旅行', '吉他']
25 };
26 var html = template('test', data);
27 document.getElementById('content').innerHTML = html;
28 </script>
29 </body>
30 </html>
 1 <!DOCTYPE HTML>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>no escape-demo</title>
 6 <script src="../dist/template.js"></script>
 7 </head>
 8 
 9 <body>
10  <h1>不转义HTML</h1>
11  <div id="content"></div>
12  <script id="test" type="text/html">
13  <p>不转义:{{#value}}</p>
14  <p>默认转义: {{value}}</p>
15  </script>
16 
17  <script>
18  var data = {
19   value: '<span style="color:#F00">hello world!</span>'
20  };
21  var html = template('test', data);
22  document.getElementById('content').innerHTML = html;
23  </script>
24 </body>
25 </html>
 1 <!DOCTYPE HTML>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>include-demo</title>
 6 <script src="../dist/template.js"></script>
 7 </head>
 8 
 9 <body>
10 <div id="content"></div>
11 <script id="test" type="text/html">
12 <h1>{{title}}</h1>
13 {{include 'list'}}
14 </script>
15 <script id="list" type="text/html">
16 <ul>
17  {{each list as value i}}
18   <li>索引 {{i + 1}} :{{value}}</li>
19  {{/each}}
20 </ul>
21 </script>
22 
23 <script>
24 var data = {
25  title: '嵌入子模板',
26  list: ['文艺', '博客', '摄影', '电影', '民谣', '旅行', '吉他']
27 };
28 var html = template('test', data);
29 document.getElementById('content').innerHTML = html;
30 </script>
31 </body>
32 </html>
 1 <!DOCTYPE HTML>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>helper-demo</title>
 6 <script src="../dist/template.js"></script>
 7 </head>
 8 
 9 <body>
10 <h1>辅助方法</h1>
11 <div id="content"></div>
12 <script id="test" type="text/html">
13 {{time | dateFormat:'yyyy年 MM月 dd日 hh:mm:ss'}}
14 </script>
15 
16 <script>
17 /**
18  * 对日期进行格式化,
19  * @param date 要格式化的日期
20  * @param format 进行格式化的模式字符串
21  *  支持的模式字母有:
22  *  y:年,
23  *  M:年中的月份(1-12),
24  *  d:月份中的天(1-31),
25  *  h:小时(0-23),
26  *  m:分(0-59),
27  *  s:秒(0-59),
28  *  S:毫秒(0-999),
29  *  q:季度(1-4)
30  * @return String
31  * @author yanis.wang
32  * @see http://yaniswang.com/frontend/2013/02/16/dateformat-performance/
33  */
34 template.helper('dateFormat', function (date, format) {
35 
36  if (typeof date === "string") {
37   var mts = date.match(/(/Date(d+)/)/);
38   if (mts && mts.length >= 3) {
39    date = parseInt(mts[2]);
40   }
41  }
42  date = new Date(date);
43  if (!date || date.toUTCString() == "Invalid Date") {
44   return "";
45  }
46 
47  var map = {
48   "M": date.getMonth() + 1, //月份
49   "d": date.getDate(), //
50   "h": date.getHours(), //小时
51   "m": date.getMinutes(), //
52   "s": date.getSeconds(), //
53   "q": Math.floor((date.getMonth() + 3) / 3), //季度
54   "S": date.getMilliseconds() //毫秒
55  };
56 
57 
58  format = format.replace(/([yMdhmsqS])+/g, function(all, t){
59   var v = map[t];
60   if(v !== undefined){
61    if(all.length > 1){
62     v = '0' + v;
63     v = v.substr(v.length-2);
64    }
65    return v;
66   }
67   else if(t === 'y'){
68    return (date.getFullYear() + '').substr(4 - all.length);
69   }
70   return all;
71  });
72  return format;
73 });
74 
75 // --------
76 
77 var data = {
78  time: 1408536771253,
79 };
80 var html = template('test', data);
81 document.getElementById('content').innerHTML = html;
82 </script>
83 </body>
84 </html>
原文地址:https://www.cnblogs.com/login123/p/12180684.html