doT.js具體使用介紹

來源:互聯網
上載者:User

標籤:graph   aar   nal   body   des   ogr   bottom   who   app   

官網:
http://olado.github.io

doT.js具體使用介紹

用法:
{{= }} 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(資料來源);


範例一:

 

1、for interpolation 賦值
格式:
{{= }}

 

資料來源:{"name":"Jake","age":31}

地區:<div id="interpolation"></div>

 

模板:

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

調用方式:

var dataInter = {"name":"Jake","age":31};
var interText = doT.template($("#interpolationtmpl").text());
$("#interpolation").html(interText(dataInter));



範例二:

2、for evaluation for in 迴圈
格式:
{{ for var key in data { }} 
{{= key }} 
{{ } }}

資料來源:{"name":"Jake","age":31,"interests":["basketball","hockey","photography"],"contact":{"email":"[email protected]","phone":"999999999"}}

地區:<div id="evaluation"></div>

模板:

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

調用方式:

var dataEval = {"name":"Jake","age":31,"interests":["basketball","hockey","photography"],"contact":{"email":"[email protected]","phone":"999999999"}};
var evalText = doT.template($("#evaluationtmpl").text());
$("#evaluation").html(evalText(dataEval));



範例三:

3、for array iteration 數組
格式:
{{~data.array :value:index }}
...
{{~}}

資料來源:{"array":["banana","apple","orange"]}

地區:<div id="arrays"></div>

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

調用方式:

var dataArr = {"array":["banana","apple","orange"]};
var arrText = doT.template($("#arraystmpl").text());
$("#arrays").html(arrText(dataArr));




範例四:

4、{{? }} for conditionals 條件
格式:
{{? }} if
{{?

?

}} else if
{{??}} else

資料來源:{"name":"Jake","age":31}

地區:<div id="condition"></div>
模板:
<script id="conditionstmpl" type="text/x-dot-template">
{{?

!it.name }}
<div>Oh, I love your name, {{=it.name}}!</div>
{{?

? !it.age === 0}}
<div>Guess nobody named you yet!</div>
{{??

}}
You are {{=it.age}} and still dont have a name?
{{?}}
</script>

調用方式:

var dataEncode = {"uri":"http://bebedo.com/?keywords=Yoga","html":"<div style=‘background: #f00; height: 30px; line-height: 30px;‘>html元素</div>"};
var EncodeText = doT.template($("#encodetmpl").text());
$("#encode").html(EncodeText(dataEncode));





範例五:

5、for interpolation with encoding
資料來源:{"uri":"http://bebedo.com/?keywords=Yoga"}

格式:
 {{!it.uri}}

地區:<div id="encode"></div>

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

調用方式:

var dataEncode = {"uri":"http://bebedo.com/?

keywords=Yoga","html":"<div style=‘background: #f00; height: 30px; line-height: 30px;‘>html元素</div>"};
var EncodeText = doT.template($("#encodetmpl").text());
$("#encode").html(EncodeText(dataEncode));




範例六:

6、{{# }} for compile-time evaluation/includes and partials
{{## #}} for compile-time defines

資料來源:{"name":"Jake","age":31}

地區:<div id="part"></div>

模板:

<script id="parttmpl" type="text/x-dot-template">
{{##def.snippet:
<div>{{=it.name}}</div>{{#def.joke}}
#}}
{{#def.snippet}}
{{=it.html}}
</script>

調用方式:

var dataPart = {"name":"Jake","age":31,"html":"<div style=‘background: #f00; height: 30px; line-height: 30px;‘>html元素</div>"};
var defPart = {"joke":"<div>{{=it.name}} who?</div>"};
var partText = doT.template($("#parttmpl").text(), undefined, defPart);
$("#part").html(partText(dataPart));


doT.js具體使用介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.