打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
JS模板引擎

参考:https://github.com/aui/artTemplate

下载后其中自带有例子;下面是自己参照写的。

  1. <!DOCTYPE HTML >
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Index</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 index}}
  15. <li>索引:{{index}}:{{value}}</li>
  16. {{/each}}
  17. </ul>
  18. {{/if}}
  19. </script>
  20. <script>
  21. var data = {
  22. title : 'HELLO WORLD',
  23. isAdmin : true,
  24. list : ['新闻','军事','历史','政治']
  25. };
  26. var html = template('test', data);
  27. document.getElementById("content").innerHTML = html;
  28. </script>
  29. <hr/>no-escape 不转义HTML
  30. <div id="div_noescape"></div>
  31. <script id="no_escape" type="text/html">
  32. <p>不转义:{{#text}}</p>
  33. <p>默认转义: {{text}}</p>
  34. </script>
  35. <script>
  36. var data_noEscape = {
  37. text: '<span style="color:#F00">hello world!</span>'
  38. };
  39. var html_noescape = template("no_escape", data_noEscape);
  40. document.getElementById("div_noescape").innerHTML = html_noescape;
  41. </script>
  42. <hr/> 在javascript中存放模板
  43. <div id="div_complie"></div>
  44. <script>
  45. var source = '<ur>' +
  46. '{{each list}}'+
  47. '<li>索引:{{$index+1}}:{{$value}}</li>'+
  48. '{{/each}}'+
  49. '</ul>';
  50. var data = {
  51. list : ['电影','电视剧','综艺','音乐']
  52. };
  53. var render = template.compile(source);
  54. var html = render(data);
  55. document.getElementById("div_complie").innerHTML = html;
  56. </script>
  57. <hr/> 嵌入子模板(include)
  58. <div id="div_include"></div>
  59. <script id="include" type="text/html">
  60. {{include 'test'}}
  61. </script>
  62. <script>
  63. document.getElementById("div_include").innerHTML = html;
  64. </script>
  65. <hr/>辅助方法
  66. <script id="helper" type="text/html">
  67. {{time | xx:'yyyy年 MM月 dd日 hh:mm:ss'}}
  68. </script>
  69. <div id="div_helper"></div>
  70. <script>
  71. /**
  72. * 对日期进行格式化,
  73. * @param date 要格式化的日期
  74. * @param format 进行格式化的模式字符串
  75. * 支持的模式字母有:
  76. * y:年,
  77. * M:年中的月份(1-12),
  78. * d:月份中的天(1-31),
  79. * h:小时(0-23),
  80. * m:分(0-59),
  81. * s:秒(0-59),
  82. * S:毫秒(0-999),
  83. * q:季度(1-4)
  84. * @return String
  85. */
  86. function dateFormat(date, format){
  87. date = new Date(date);
  88. var map = {
  89. "M": date.getMonth() + 1, //月份
  90. "d": date.getDate(), //日
  91. "h": date.getHours(), //小时
  92. "m": date.getMinutes(), //分
  93. "s": date.getSeconds(), //秒
  94. "q": Math.floor((date.getMonth() + 3) / 3), //季度
  95. "S": date.getMilliseconds() //毫秒
  96. };
  97. format = format.replace(/([yMdhmsqS])+/g, function(all, t){
  98. var v = map[t];
  99. if (v !== undefined) {
  100. if (all.length > 1) {
  101. v = '0' + v;
  102. v = v.substr(v.length - 2);
  103. }
  104. return v;
  105. }
  106. else if (t === 'y') {
  107. return (date.getFullYear() + '').substr(4 - all.length);
  108. }
  109. return all;
  110. });
  111. return format;
  112. }
  113. var data = {
  114. time: 1408536771253,
  115. };
  116. template.helper("xx", dateFormat);
  117. var html = template('helper', data);
  118. document.getElementById('div_helper').innerHTML = html;
  119. // document.getElementById("div_helper").innerHTML = dateFormat(new Date());
  120. </script>
  121. </body>
  122. </html>

helper:
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Index</title>
  6. <script src="dist/template.js">
  7. </script>
  8. </head>
  9. <body>
  10. <div id="content">
  11. </div>
  12. <script id="test" type="text/html">
  13. {{xx(n)}}
  14. </script>
  15. <script>
  16. template.helper("xx", function(a){
  17. return 10 + "--" + a;
  18. });
  19. var data = {
  20. n: 123
  21. };
  22. var html = template("test", data);
  23. document.getElementById("content").innerHTML = html;
  24. </script>
  25. </div>
  26. </body>
  27. </html>

其中函数参考:http://yaniswang.com/frontend/2013/02/16/dateformat-performance/
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
EJS入门
HTML DOM
innerText 与 innerHtml的区别 (一)
JavaScript基础
动态解析HTML中的Javascript
新办法绕过xss过滤-让xss来的更猛烈些吧
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服