4、模板插件
doT.min.js
链接: http://www.jq22.com/jquery-info8648
(1)调用方式:
var tmpText = doT.template(模板); tmpText(数据源);
(2)使用:
区域:
<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));
(3)语法
{{= }} 赋值
{{ }} 循环对象
{{ for var key in data { }} {{= key }} {{ } }}
{{~ }} 循环数组
{{~it :v:i}}
//[ ]
数据源:{"array":["banana","apple","orange"]}
//{ "ss": [ ] }
区域: <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));
{{? }} if
{{?? }} else if
{{??}} else
{{! }} for interpolation with encoding
{{# }} for compile-time evaluation/includes and partials
{{## #}} for compile-time defines