doT.js使用笔记

doT.js

doT.js github地址

Created in search of the fastest and concise JavaScript templating function with emphasis on performance under V8 and nodejs. It shows great performance for both nodejs and browsers.
doT.js is fast, small and has no dependencies.
  • 使用方法:

{{= }}
// for interpolation
{{ }} 
//for evaluation
{{~ }} 
//for array iteration
{{? }}
// for conditionals
{{! }}
// for interpolation with encoding
{{# }}
// for compile-time evaluation/includes and partials
{{## #}}
// for compile-time defines
  • 调用方式:

var tmpText = doT.template(模板);
tmpText(数据源);
  • 语法结构:

赋值:

//格式:
{{= }}
//示例:
<div id="show_list"></div>

<script id="tpl" type="text/x-dot-template">
      <div>Hi {{=it.name}}!</div>
      <div>{{=it.age || ''}}</div>
</script>

<script>
     var data = {"name":"Jake","age":31};
     var show_tpl = doT.template($("#tpl").text());
     $("#show_list").html(show_tpl(data));
</script>

for 循环结构:

//格式:
{{ for(var x in data) { }} 
    {{= key }} 
{{ } }}
//示例:

<div id="show_list"></div>

<script id="tpl" type="text/x-dot-template">
     {{ for(var x in it) { }}
          <div>KEY:{{= x }}---VALUE:{{= it[x] }}</div>
     {{ } }}
</script>

<script>
var data = {
              "name":"Jake",
              "age":31,
              "interests":["basketball","hockey","photography"],
              "contact":{
                          "email":"jake@xyz.com",
                          "phone":"999999999"
               }
      };  
     var show_tpl = doT.template($("#tpl").text());
     $("#show_list").html(show_tpl(data));
</script>

if 逻辑结构:

//格式:
{{if(conditions){ }}
{{} eles if(conditions){ }}
{{} eles{ }}
{{ }}} 
//示例:

<div id="show_list"></div>

<script id="tpl" type="text/x-dot-template">
      {{if(!it.name) { }}
            <div>Oh, I love your name, {{=it.name}}!</div>
          {{ } eles if(!it.age === 0) { }}
                <div>Guess nobody named you yet!</div>
          {{} eles{ }}
                You are {{=it.age}} and still dont have a name?
      {{ }}} 
</script>

<script>
var data = {
              "name":"Jake",
              "age":31,
              "interests":["basketball","hockey","photography"],
              "contact":{
                          "email":"jake@xyz.com",
                          "phone":"999999999"
               }
      };  
     var show_tpl = doT.template($("#tpl").text());
     $("#show_list").html(show_tpl(data));
</script>

数组:

//格式:
{{~data.array :value:index }}
//示例:

<div id="show_list"></div>

<script id="tpl" type="text/x-dot-template">
      {{~it.array:value:index}}
          <div>{{= index+1 }}{{= value }}!</div>
</script>

<script>
var data = {
            "array":["banana","apple","orange"]
      };
     var show_tpl = doT.template($("#tpl").text());
     $("#show_list").html(show_tpl(data));
</script>

编码:

//格式:
{{!it.uri}}
//示例:

<div id="show_list"></div>

<script id="tpl" type="text/x-dot-template">
     Visit {{!it.uri}} {{!it.html}}
</script>

<script>
var data = {
           "uri":"http://bebedo.com/?keywords=Yoga",
           "html":"<div style='background: #f00; height: 30px'>html元素</div>"
      };
     var show_tpl= doT.template($("#tpl").text());
     $("#show_list").html(show_tpl(data));
</script>


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 引言 人真不应该太闲,闲久了就会胡思乱想。虽说人是一根会思想的芦苇,但毕竟想多了就会多了几分踌躇,想多了就会倍加脆...
    佐罗奇士阅读 470评论 0 0
  • 身坐在教室 心飞到电视机 时光飞逝 成就仍无 哎~ 日子难度 相知此诗作者为谁? 高二三班大白痴 ———被我翻出来...
    kelly的小创作阅读 201评论 0 1
  • 天气凉了,爸妈也快明天也要回家了。为了我,他们付出了很多。
    嘟嘟_7ccb阅读 196评论 0 0
  • 寒假在家闲来无事,刷微博无意发现了简书,感觉就像走进了一个奇妙的世界,原来生活不止眼前的无聊,还有臭味相投的一批人...
    在下小星阅读 290评论 1 5