doT.js - 图1

doT.js github地址 前端渲染引擎doT.js解析

使用方法:

  1. {{= }}
  2. // for interpolation 用于插值
  3. {{ }}
  4. //for evaluation
  5. {{~ }}
  6. //for array iteration 用于数组迭代
  7. {{? }}
  8. // for conditionals 用于条件
  9. {{! }}
  10. // for interpolation with encoding 用于带编码的插值
  11. {{# }}
  12. // for compile-time evaluation/includes and partials
  13. {{## #}}
  14. // for compile-time defines

调用方式:

  1. var tmpText = doT.template(模板);
  2. tmpText(数据源);

语法结构:

赋值:

  1. //格式:
  2. {{= }}
  3. //示例:
  4. <div id="show_list"></div>
  5. <script id="tpl" type="text/x-dot-template">
  6. <div>Hi {{=it.name}}!</div>
  7. <div>{{=it.age || ''}}</div>
  8. </script>
  9. <script>
  10. var data = {"name":"Jake","age":31};
  11. var show_tpl = doT.template($("#tpl").text());
  12. $("#show_list").html(show_tpl(data));
  13. </script>

for 循环结构:

  1. //格式:
  2. {{ for(var x in data) { }}
  3. {{= key }}
  4. {{ } }}
  5. //示例:
  6. <div id="show_list"></div>
  7. <script id="tpl" type="text/x-dot-template">
  8. {{ for(var x in it) { }}
  9. <div>KEY:{{= x }}---VALUE:{{= it[x] }}</div>
  10. {{ } }}
  11. </script>
  12. <script>
  13. var data = {
  14. "name":"Jake",
  15. "age":31,
  16. "interests":["basketball","hockey","photography"],
  17. "contact":{
  18. "email":"jake@xyz.com",
  19. "phone":"999999999"
  20. }
  21. };
  22. var show_tpl = doT.template($("#tpl").text());
  23. $("#show_list").html(show_tpl(data));
  24. </script>

if 逻辑结构:

  1. //格式:
  2. {{if(conditions){ }}
  3. {{} eles if(conditions){ }}
  4. {{} eles{ }}
  5. {{ }}}
  6. //示例:
  7. <div id="show_list"></div>
  8. <script id="tpl" type="text/x-dot-template">
  9. {{if(!it.name) { }}
  10. <div>Oh, I love your name, {{=it.name}}!</div>
  11. {{ } eles if(!it.age === 0) { }}
  12. <div>Guess nobody named you yet!</div>
  13. {{} eles{ }}
  14. You are {{=it.age}} and still dont have a name?
  15. {{ }}}
  16. </script>
  17. <script>
  18. var data = {
  19. "name":"Jake",
  20. "age":31,
  21. "interests":["basketball","hockey","photography"],
  22. "contact":{
  23. "email":"jake@xyz.com",
  24. "phone":"999999999"
  25. }
  26. };
  27. var show_tpl = doT.template($("#tpl").text());
  28. $("#show_list").html(show_tpl(data));
  29. </script>

数组:

  1. //格式:
  2. {{~data.array :value:index }}
  3. //示例:
  4. <div id="show_list"></div>
  5. <script id="tpl" type="text/x-dot-template">
  6. {{~it.array:value:index}}
  7. <div>{{= index+1 }}{{= value }}!</div>
  8. </script>
  9. <script>
  10. var data = {
  11. "array":["banana","apple","orange"]
  12. };
  13. var show_tpl = doT.template($("#tpl").text());
  14. $("#show_list").html(show_tpl(data));
  15. </script>

编码:

  1. //格式:
  2. {{!it.uri}}
  3. //示例:
  4. <div id="show_list"></div>
  5. <script id="tpl" type="text/x-dot-template">
  6. Visit {{!it.uri}} {{!it.html}}
  7. </script>
  8. <script>
  9. var data = {
  10. "uri":"http://bebedo.com/?keywords=Yoga",
  11. "html":"<div style='background: #f00; height: 30px'>html元素</div>"
  12. };
  13. var show_tpl= doT.template($("#tpl").text());
  14. $("#show_list").html(show_tpl(data));
  15. </script>

doT.min.js