JavaScript模板引擎Template.js使用詳解


這篇文章主要為大家詳細介紹了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 {{if admin}}
2  <p>admin</p>
3 {{else if code > 0}}
4  <p>master</p>
5 {{else}}
6  <p>error!</p>
7 {{/if}}

5)、遍歷表達式

無論數組或者對象都可以用 each 進行遍歷。

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

亦可以被簡寫:

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

6)、模板包含表達式

用於嵌入子模板。

{{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 });

模板中使用的方式: {{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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM