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(數(shù)據(jù)源);
  • 語法結構:

賦值:

//格式:
{{= }}
//示例:
<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 循環(huán)結構:

//格式:
{{ 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>

數(shù)組:

//格式:
{{~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>


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 引言 人真不應該太閑,閑久了就會胡思亂想。雖說人是一根會思想的蘆葦,但畢竟想多了就會多了幾分躊躇,想多了就會倍加脆...
    佐羅奇士閱讀 511評論 0 0
  • 身坐在教室 心飛到電視機 時光飛逝 成就仍無 哎~ 日子難度 相知此詩作者為誰? 高二三班大白癡 ———被我翻出來...
    kelly的小創(chuàng)作閱讀 227評論 0 1
  • 天氣涼了,爸媽也快明天也要回家了。為了我,他們付出了很多。
    嘟嘟_7ccb閱讀 229評論 0 0
  • 寒假在家閑來無事,刷微博無意發(fā)現(xiàn)了簡書,感覺就像走進了一個奇妙的世界,原來生活不止眼前的無聊,還有臭味相投的一批人...
    在下小星閱讀 341評論 1 5

友情鏈接更多精彩內容