基於jQuery開發的javascript模板引擎-jTemplates


這里介紹一個基於jQuery開發的模板引擎。
jTemplates目前最新的版本是0.7.8,由tPython開發。官方網站:http://jtemplates.tpython.com

兩個附件,一個是jTemplates官方網站提供的下載包,其中包括jTemplates的說明、jTemplates JS庫、jTemplates DOC。
          另一個是使用jTemplates做的三個DEMO。

一 , 簡單介紹

它是一個基於jQuery開發的javascript模板引擎。它主要的作用如下:

1. 通過JavaScript獲取JSON形式的數據;

2. 獲取一個HTML模板,與數據相結合,生成頁面HTML。

二 , 快速上手

先來看一個簡單的例子:

Js代碼   收藏代碼
  1. <script type="text/javascript" src="jquery-1.2.6.pack.js"></script>  
  2. <script type="text/javascript" src="jquery-jtemplates.js"></script>  
  3.   
  4. <script type="text/javascript">  
  5.    $(document).ready(function() {  
  6.     //初始化數據  
  7.     var data = {  
  8.      name: '用戶列表',  
  9.      list_id: '編號89757',  
  10.      table: [  
  11.       {id: 1, name: 'Rain', age: 22, mail: 'cssrain@domain.com'},  
  12.       {id: 2, name: '皮特', age: 24, mail: 'cssrain@domain.com'},  
  13.       {id: 3, name: '卡卡', age: 20, mail: 'cssrain@domain.com'},  
  14.       {id: 4, name: '戲戲', age: 26, mail: 'cssrain@domain.com'},  
  15.       {id: 5, name: '一揪', age: 25, mail: 'cssrain@domain.com'}  
  16.      ]  
  17.     };  
  18.     // 附上模板  
  19.     $("#result1").setTemplateElement("template");  
  20.     // 給模板加載數據  
  21.     $("#result1").processTemplate(data);  
  22.    });   
  23. </script>  
  24.   
  25. <!-- 模板內容 -->  
  26. <textarea id="template" style="display:none">  
  27.    <strong>{$T.name} : {$T.list_id}</strong>  
  28.    <table>  
  29.         <tr>  
  30.       <th>編號</th>  
  31.       <th>姓名</th>  
  32.                     <th>年齡</th>  
  33.       <th>郵箱</th>  
  34.     </tr>  
  35.     {#foreach $T.table as record}  
  36.     <tr>  
  37.       <td>{$T.record.id}</td>  
  38.       <td>{$T.record.name}</td>  
  39.                     <td>{$T.record.age}</td>  
  40.       <td>{$T.record.mail}</td>  
  41.     </tr>  
  42.     {#/for}  
  43.    </table>  
  44. </textarea>  
  45.   
  46. <!-- 輸出元素 -->  
  47. <div id="result1" ></div>  



上面代碼中,先導入了jQuery.js,然后導入jtemplates.js。

接下來新建了一個data數據的json對象。

var data = {
     ......
};

然后在HTMl頁面上增加一個 輸出元素 和 一個模板元素:

最后在JS給輸出元素 附加模板 和 數據。

這樣,運行代碼后,出現如下圖所示界面。

用戶列表 : 編號89757
編號   姓名  年齡  郵箱
1        Rain   22    cssrain@domain.com
2        皮特   24   cssrain@domain.com
3       卡卡   20    cssrain@domain.com
4       戲戲   26    cssrain@domain.com
5       一揪   25    cssrain@domain.com



三 , 加載模板

這次把模板放到一個單獨的頁面中,而不是直接寫在頁面里。

新建一個template.html,放入以下代碼:

Js代碼   收藏代碼
  1. <strong>{$T.name} : {$T.list_id}</strong>  
  2. <table>  
  3.     <tr>  
  4.     <th>編號</th>  
  5.     <th>姓名</th>  
  6.             <th>年齡</th>  
  7.     <th>郵箱</th>  
  8. </tr>  
  9. {#foreach $T.table as record}  
  10. <tr>  
  11.     <td>{$T.record.id}</td>  
  12.     <td>{$T.record.name}</td>  
  13.             <td>{$T.record.age}</td>  
  14.     <td>{$T.record.mail}</td>  
  15. </tr>  
  16. {#/for}  
  17. </table>  


然后新建一個json文件,名稱為cs.json,代碼如下:

Js代碼   收藏代碼
  1. {  
  2. name: "用戶列表",  
  3. list_id: "編號89757",  
  4.     table: [  
  5.      {id: 1, name: 'Rain', age: 22, mail: 'cssrain@domain.com'},  
  6.      {id: 2, name: '皮特', age: 24, mail: 'cssrain@domain.com'},  
  7.      {id: 3, name: '卡卡', age: 20, mail: 'cssrain@domain.com'},  
  8.      {id: 4, name: '戲戲', age: 26, mail: 'cssrain@domain.com'},  
  9.      {id: 5, name: '一揪', age: 25, mail: 'cssrain@domain.com'}  
  10. ]  
  11. }  


在jQuery中,可以通過$.ajax()方法來加載 json文件,代碼如下:

Js代碼   收藏代碼
  1. <script type="text/javascript">  
  2. $(function(){  
  3. $.ajax({  
  4.    type:       "post",  
  5.    dataType:   "json",  
  6.    url:        "cs.json",  
  7. success:    function(data){  
  8.                     .....  
  9.                 }  
  10. });  
  11. });  
  12. </script>  


在success回調函數里,首先需要設置模板,然后需要添加數據。如下代碼所示:

Js代碼   收藏代碼
  1. success:    function(data){  
  2.   
  3.                     // 設置模板  
  4.                    $("#result1").setTemplateURL("template.html?date="+(+new Date()), null, {filter_data: true});  
  5.                     //   加載數據  
  6.                     $("#result1").processTemplate(data);  
  7.                 }  
  8. }  


完整代碼如下所示:

Js代碼   收藏代碼
  1. $(function(){  
  2. $.ajax({  
  3.    type:       "post",  
  4.    dataType:   "json",  
  5.    url:        "cs.json",  
  6. success:    function(data){  
  7.                     $("#result1").setTemplateURL("template.html?date="+(+new Date()), null, {filter_data: true});  
  8.                     $("#result1").processTemplate(data);  
  9.                 }  
  10. });  
  11. }); 


免責聲明!

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



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