doT模板方便快捷的组织页面DOM > doT.js特点是快,小,无依赖其他插件。压缩版仅有4K大小,最近使用dot的时候整理出这个dot.js教程api文档,是对dot.js的介绍和实例,希望能帮助到一部分需要的人。 ***** [TOC] #### 使用方法: ``` {{= }} 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 赋值 格式: `{{= }}` 例子: ``` <div id="interpolation"></div> <script id="interpolationtmpl" type="text/x-dot-template"> <div>Hi {{=it.name}}!</div> <div>{{=it.age || ''}}</div> </script> <script> var dataInter = {"name":"Jake","age":31}; var interText = doT.template($("#interpolationtmpl").text()); $("#interpolation").html(interText(dataInter)); </script> ``` ***** ### 示例2:for evaluation for in 循环 格式: ``` {{ for var key in data { }} {{= key }} {{ } }} ``` 例子: ``` <div id="interpolation"></div> <script id="interpolationtmpl" type="text/x-dot-template"> {{ for(var prop in it) { }} <div>KEY:{{= prop }}---VALUE:{{= it[prop] }}</div> {{ } }} </script> <script> var dataInter ={"name":"Jake","age":31,"interests":["basketball","hockey","photography"],"contact":{"email":"jake@xyz.com","phone":"999999999"}}; var interText = doT.template($("#interpolationtmpl").text()); $("#interpolation").html(interText(dataInter)); </script> ``` ### 示例3:{{~ }} for array iteration 数组 格式: ``` {{~data.array :value:index }} ... {{~}} ``` 例子: ``` <div id="interpolation"></div> <script id="interpolationtmpl" type="text/x-dot-template"> {{~it.array:value:index}} <div>{{= index+1 }}{{= value }}!</div> {{~}} </script> <script> var dataInter ={"array":["banana","apple","orange"]}; var interText = doT.template($("#interpolationtmpl").text()); $("#interpolation").html(interText(dataInter)); </script> ``` ### 示例4:{{? }} for conditionals 条件 格式: ``` {{? }} if {{?? }} else if {{??}} else ``` 例子: ``` <div id="interpolation"></div> <script id="interpolationtmpl" 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> <script> var dataInter ={"name":"Jake","age":31}; var interText = doT.template($("#interpolationtmpl").text()); $("#interpolation").html(interText(dataInter)); </script> ``` ### 示例5:for interpolation with encoding 格式: ``` {{!it.uri}} ``` 例子: ``` <div id="interpolation"></div> <script id="interpolationtmpl" type="text/x-dot-template"> Visit {{!it.uri}} {{!it.html}} </script> <script> var dataInter ={"uri":"http://jq22.com/?keywords=Yoga","html":"<div style='background: #f00; height: 30px; line-height: 30px;'>html元素</div>"}; var interText = doT.template($("#interpolationtmpl").text()); $("#interpolation").html(interText(dataInter)); </script> ``` ### 示例6:{{# }} for compile-time evaluation/includes and partials {{## #}} for compile-time defines 例子: ``` <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> <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)); </script> ```