推荐好用的Jquery模板插件Jtemplate

来源:互联网 发布:javascript在线翻译 编辑:程序博客网 时间:2024/04/29 07:30

jtemplate是javascript的模板引擎。官方网址:http://jtemplates.tpython.com/


数据准备:

view plainprint?
  1. var data ={  
  2. TotalCount:64,  
  3. Lists:[  
  4. {Id:'2001' ,Title:'新闻11',CreateDate:'2011-08-08'},  
  5. {Id:'2002' ,Title:'新闻22',CreateDate:'2011-08-08'},  
  6. {Id:'2003' ,Title:'新闻33',CreateDate:'2011-08-08'},  
  7. {Id:'2004' ,Title:'新闻44',CreateDate:'2011-08-08'},  
  8. {Id:'2005' ,Title:'新闻55',CreateDate:'2011-08-08'},  
  9. ]  
  10. }  


1、引入库文件

view plainprint?
  1. <script type="text/javascript" src="jquery.js"></script>  
  2. <script type="text/javascript" src="jquery-jtemplates.js"></script>  


2、编写模板

view plainprint?
  1. <div id="<span style="color:#FF0000;">result</span>"></div>  
  2. <div id="templateContainer" style="display:none;">  
  3. <table>  
  4. <tr><td>Id</td><td>标题</td><td>发布时间</td></tr>  
  5. {#foreach $T.table as row}  
  6. <tr><td>{$T.row.Id}</td><td>{$T.row.Title}</td><td>{$T.row.CreateDate}</td></tr>  
  7. {#/for}  
  8. </table>  
  9. </div>  

语法:

1、大括号{..} ,在这里面可以写任何javascript的代码,比如 {$T.toUpperCase()}

2、{$T} : 表示数据,例如上面的例子,$T.table表示得到data的table对象,$T.TotalCount 为 64。

3、{#foreach} : 循环获取数据,如上面:{#foreach $T.table as row}      {$T.row.Title}      {/for}   


扩展语法:

{#if}

例子:
view plainprint?
  1. {#if $T=="true"} good {#else} fail {#/if}  
view plainprint?
  1. {#if $T.list_id == 3} System list {#elseif $T.list_id == 4} Users List {#elseif $T.list_id == 5} Errors list {#/if}  


{#foreach}
view plainprint?
  1. {#foreach |VAR| as |NAME| [begin=|CODE|] [count=|CODE|] [step=|CODE|]}..{#else}..{#/for}  


例子:
a、输出所有数据:
view plainprint?
  1. {#foreach $T.table as row}      {$T.row.Title}      {/for}     

b、从第二条记录开始输出:
view plainprint?
  1. {#foreach $T.table as row begin=1}      {$T.row.Title}      {/for}     

c、从第二条开始且只取2条
view plainprint?
  1. {#foreach $T.table as row begin=1 count=2}      {$T.row.Title}      {/for}     

d、使用step
view plainprint?
  1. {#foreach $T.table as row step=2}      {$T.row.Title}      {/for}   

e、使用else

view plainprint?
  1. {#foreach $T.table as row step=2}      {$T.row.Title}  {#else}   无记录   {/for}   


{#for}
例子:

view plainprint?
  1. {#for index = 1 to 10} {$T.index} {#/for}  
view plainprint?
  1. {#for index = 1 to 10 step=3} {$T.index} {#/for}  



3、渲染模板并展示


view plainprint?
  1. <script type="text/javascript">   
  2.         $(document).ready(function() {  
  3.             // 设置模板  
  4.             $("#result").setTemplateElement("templateContainer");  
  5.               
  6.             // 处理模板  
  7.             $("#result").processTemplate(data);  
  8.         });   
  9.     </script>   

设置模板的几种方法:

a. setTemplateElement:参数为页面中的一个元素ID
如上面的例子

b. setTemplate: 参数为具体的模板内容,
如:$("#result").setTemplate("Template by {$T.bold()} version <em>{$Q.version}</em>.");

c.setTemplateURL:使用外部独立模板文件Url作为参数
如:$("#result").setTemplateURL("example_multitemplate1.tpl");


4、运行结果:



完整代码
view plainprint?
  1. <html>   
  2. <head>   
  3.   
  4.     <script type="text/javascript" src="jquery.js"></script>  
  5.   
  6.     <script type="text/javascript" src="jquery-jtemplates.js"></script>  
  7.   
  8.     <title>jTemplates</title>  
  9.   
  10.     <script type="text/javascript">   
  11.         var data ={  
  12.                 TotalCount:64,  
  13.                 Lists:[  
  14.                     {Id:'2001' ,Title:'新闻11',CreateDate:'2011-08-08'},  
  15.                     {Id:'2002' ,Title:'新闻22',CreateDate:'2011-08-08'},  
  16.                     {Id:'2003' ,Title:'新闻33',CreateDate:'2011-08-08'},  
  17.                     {Id:'2004' ,Title:'新闻44',CreateDate:'2011-08-08'},  
  18.                     {Id:'2005' ,Title:'新闻55',CreateDate:'2011-08-08'},  
  19.                 ]  
  20.         };  
  21.         $(document).ready(function() {  
  22.             // 设置模板  
  23.             $("#result").setTemplateElement("templateContainer");  
  24.               
  25.             // 处理模板  
  26.             $("#result").processTemplate(data);  
  27.         });   
  28.     </script>  
  29.   
  30. </head>  
  31. <body>  
  32.     <div id="result">  
  33.     </div>  
  34.     <textarea id="templateContainer" style="display: none;">  
  35.         <table border="1">  
  36.             <tr>  
  37.                 <td>  
  38.                     Id  
  39.                 </td>  
  40.                 <td>  
  41.                     标题  
  42.                 </td>  
  43.                 <td>  
  44.                     发布时间  
  45.                 </td>  
  46.             </tr>  
  47.             {#foreach $T.Lists as row}  
  48.             <tr>  
  49.                 <td>  
  50.                     {$T.row.Id}  
  51.                 </td>  
  52.                 <td>  
  53.                     {$T.row.Title}  
  54.                 </td>  
  55.                 <td>  
  56.                     {$T.row.CreateDate}  
  57.                 </td>  
  58.             </tr>  
  59.             {#/for}  
  60.         </table>  
  61.     </textarea>  
  62. </body>  
  63. </html>  
原创粉丝点击