ArtTemplate使用笔记

来源:互联网 发布:c语言乘法口诀 编辑:程序博客网 时间:2024/04/25 06:02
1.basic

[html] view plaincopyprint?
  1. <!DOCTYPE HTML>  
  2. <html><head><meta charset="UTF-8"><title>demo</title>  
  3. <script src="../template.js"></script>  
  4. </head>  
  5. <body>  
  6. <div id="content"></div>  
  7. <script id="test" type="text/html">  
  8. <% if (isAdmin) { %>  
  9.   
  10. <h1><%=title%></h1>  
  11. <ul>  
  12.     <% for (var i = 0; i < list.length; i ++) { %>  
  13.         <li>索引 <%= i + 1 %><%= list[i] %></li>  
  14.     <% } %>  
  15. </ul>  
  16.   
  17. <% } %>  
  18. </script>  
  19.   
  20. <script>  
  21. var data = {  
  22.     title: '基本例子',  
  23.     isAdmin: true,  
  24.     list: ['文艺', '博客', '摄影', '电影', '民谣', '旅行', '吉他']  
  25. };  
  26. var html = template.render('test', data);  
  27. document.getElementById('content').innerHTML = html;  
  28. </script>  
  29. </body>  
  30. </html>  

2.使用语法工具

[html] view plaincopyprint?
  1. <!DOCTYPE HTML>  
  2. <html><head><meta charset="UTF-8"><title>demo</title>  
  3. <script src="../template.js"></script>  
  4. <script src="../extensions/template-syntax.js"></script>  
  5. <script id="test" type="text/html">  
  6. {if isAdmin}   
  7.   
  8. <h1>{title}</h1>  
  9. <ul>  
  10. {each list}  
  11.    <li>索引{$index}. {$value}</li>  
  12. {/each}  
  13. </ul>  
  14.   
  15. {/if}  
  16. </script>  
  17. <script>  
  18. var data = {  
  19.     title: '基本例子',  
  20.     isAdmin: true,  
  21.     list: ['文艺', '博客', '摄影', '电影', '民谣', '旅行', '吉他']  
  22. };  
  23. var html = template.render('test', data);  
  24. document.getElementById('content').innerHTML = html;  
  25. </script>  
  26. </body>  
  27. </html>  


3.循环each的另一种写法——{each list as item index },这种很有用,我后来才知道。

[html] view plaincopyprint?
  1. <!DOCTYPE HTML>  
  2. <html><head><meta charset="UTF-8"><title>demo</title>  
  3. <script src="../template.js"></script>  
  4. <script src="../extensions/template-syntax.js"></script>  
  5. </head>  
  6. <body>  
  7. <div id="content"></div>  
  8. <script id="test" type="text/html">  
  9. {if isAdmin}   
  10.   
  11. <h1>{title}</h1>  
  12. <ul>  
  13.     {each list as item index}  
  14.         <li>索引index.item</li>  
  15.     {/each}  
  16. </ul>  
  17.   
  18. {/if}  
  19. </script>  
  20.   
  21. <script>  
  22. var data = {  
  23.     title: '基本例子',  
  24.     isAdmin: true,  
  25.     list: ['文艺', '博客', '摄影', '电影', '民谣', '旅行', '吉他']  
  26. };  
  27. var html = template.render('test', data);  
  28. document.getElementById('content').innerHTML = html;  
  29. </script>  
  30. </body>  
  31. </html>  


4.使用外部模板——include。

基本语法:

1.原始写法 <%include('外部模板名',[可选数据])%>;

2.使用语法工具 {include '外部模板名'  [可选数据] }。

这里的可选数据在糖饼的文档和例子里没有具体介绍,但是根据源码分析后,可以按照下面方式操作。

[html] view plaincopyprint?
  1. <!DOCTYPE HTML>  
  2. <html><head><meta charset="UTF-8"><title>demo</title>  
  3. <script src="../template.js"></script>  
  4. <script src="../extensions/template-syntax.js"></script>  
  5. </head>  
  6. <body>  
  7. <table id="content"></table>  
  8. <script id="inside" type="text/html">  
  9.     {each list as insideItem insideIndex}  
  10.         <li>索引 {echo insideIndex} {echo insideItem.name}</li>  
  11.     {/each}  
  12. </script>  
  13.   
  14. <script id="outside" type="text/html">  
  15. {each example as item index }  
  16.       
  17.     {if item.isAdmin}  
  18.         <tr>  
  19.             <td>true:{echo item.title}</td>  
  20.             <td>  
  21.                 {include 'inside' item}  
  22.             </td>  
  23.         </tr>  
  24.     {else}  
  25.         <tr>  
  26.             <td>false:{echo item.title}</td>  
  27.             <td>  
  28.                 {include 'inside' item}  
  29.             </td>  
  30.         </tr>  
  31.     {/if}  
  32.       
  33. {/each}  
  34. </script>  
  35. <script>  
  36. /*  
  37.     {$index}: {$value.name}  
  38. */  
  39. var data = {  
  40.     example:  
  41.         [{  
  42.             title: '基本例子1',  
  43.             isAdmin: true,  
  44.             list: [  
  45.                 {name:'文艺1'},   
  46.                 {name:'博客1'},   
  47.                 {name:'摄影1'},   
  48.                 {name:'电影22'},   
  49.                 {name:'民谣1'},   
  50.                 {name:'旅行1'},   
  51.                 {name:'吉他1'}  
  52.             ]  
  53.         },{  
  54.             title: '基本例子2',  
  55.             isAdmin: false,  
  56.             list: [  
  57.                 {name:'文艺2'},   
  58.                 {name:'博客2'},   
  59.                 {name:'摄影2'},   
  60.                 {name:'电影2'},   
  61.                 {name:'民谣3'},   
  62.                 {name:'旅行2'},   
  63.                 {name:'吉他2'}  
  64.             ]  
  65.         }]  
  66.       
  67.     };  
  68. var html = template.render('outside', data);  
  69. document.getElementById('content').innerHTML = html;  
  70. </script>  
  71. </body>  
0 0
原创粉丝点击