doT.js特色是快,小,無依賴其餘插件。壓縮版僅有4K大小,最近使用點的時候整理出這個dot.js教程API文檔,是對dot.js的介紹和實例,但願能幫助到一部分須要的人。html
1
2
3
4
五
6
7
|
{{= }} for interpolation
{{ }} for evaluation
{{~ }} for array iteration
{{? }} for conditionals
{{! }} for interpolation with encoding
{{# }} for compile-time evaluation/includes and partials
{{## #}} for compile-time defines
|
1
2
|
var tmpText = doT.template(模板);
tmpText(數據源);
|
例子一:數組
1,用於插值賦值app
格式:編碼
1
|
{{= }}
|
數據源:lua
1
|
{"name":"Jake","age":31}
|
區域:spa
1
|
<
div
id
=
"interpolation"
></
div
>
|
1
2
3
4
|
<script id=
"interpolationtmpl"
type=
"text/x-dot-template"
>
<div>Hi {{=it.name}}!</div>
<div>{{=it.age ||
''
}}</div>
</script>
|
調用方式:插件
1
2
3
|
var
dataInter = {
"name"
:
"Jake"
,
"age"
:31};
var
interText = doT.template($(
"#interpolationtmpl"
).text());
$(
"#interpolation"
).html(interText(dataInter));
|
例子二:rest
2,用於評估循環code
格式:htm
1
2
3
|
{{
for
var
key
in
data { }}
{{= key }}
{{ } }}
|
數據源:
1
|
{"name":"Jake","age":31,"interests":["basketball","hockey","photography"],"contact":{"email":"jake@xyz.com","phone":"999999999"}}
|
區域:
1
|
<
div
id
=
"evaluation"
></
div
>
|
模板:
1
2
3
4
五
|
<script id=
"evaluationtmpl"
type=
"text/x-dot-template"
>
{{
for
(
var
prop
in
it) { }}
<div>KEY:{{= prop }}---VALUE:{{= it[prop] }}</div>
{{ } }}
</script>
|
調用方式:
1
2
3
|
var
dataEval = {
"name"
:
"Jake"
,
"age"
:31,
"interests"
:[
"basketball"
,
"hockey"
,
"photography"
],
"contact"
:{
"email"
:
"jake@xyz.com"
,
"phone"
:
"999999999"
}};
var
evalText = doT.template($(
"#evaluationtmpl"
).text());
$(
"#evaluation"
).html(evalText(dataEval));
|
例子三:
3,用於數組迭代的數組
格式:
1
2
3
|
{{~data.array :value:index }}
...
{{~}}
|
數據源:
1
|
{
"array"
:[
"banana"
,
"apple"
,
"orange"
]}
|
區域:
1
|
<
div
id
=
"arrays"
></
div
>
|
模板:
1
2
3
4
五
|
<script id=
"arraystmpl"
type=
"text/x-dot-template"
>
{{~it.array:value:index}}
<div>{{= index+1 }}{{= value }}!</div>
{{~}}
</script>
|
調用方式:
1
2
3
|
var
dataArr = {
"array"
:[
"banana"
,
"apple"
,
"orange"
]};
var
arrText = doT.template($(
"#arraystmpl"
).text());
$(
"#arrays"
).html(arrText(dataArr));
|
例子四:
4,{{?條件條件
格式:
1
2
3
|
{{? }}
if
{{?? }}
else
if
{{??}}
else
|
數據源:
1
|
{"name":"Jake","age":31}
|
區域:
1
|
<
div
id
=
"condition"
></
div
>
|
模板:
1
2
3
4
五
6
7
8
9
|
<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>
|
調用方式:
1
2
3
|
var
dataEncode = {
"uri"
:
"http://jq22.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,用於帶編碼的插值
數據源:
1
|
{"uri":"http://jq22.com/?keywords=Yoga"}
|
格式:
1
|
{{!it.uri}}
|
區域:
1
|
<
div
id
=
"encode"
></
div
>
|
模板:
1
2
3
|
<script id=
"encodetmpl"
type=
"text/x-dot-template"
>
Visit {{!it.uri}} {{!it.html}}
</script>
|
調用方式:
1
2
3
|
var
dataEncode = {
"uri"
:
"http://jq22.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,{{#}}用於編譯時評估/包含和部分
{{###}}用於編譯時定義
數據源:
1
|
{"name":"Jake","age":31}
|
區域:
1
|
<
div
id
=
"part"
></
div
>
|
模板:
1
2
3
4
五
6
7
|
<script id=
"parttmpl"
type=
"text/x-dot-template"
>
{{
##def.snippet:
<div>{{=it.name}}</div>{{
#def.joke}}
#}}
{{
#def.snippet}}
{{=it.html}}
</script>
|
調用方式:
1
2
3
4
|
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));
|