doT js模板入門 2
阿新 • • 發佈:2018-03-28
管家 click 而不是 date .cn appname rowspan time -c
doT js 使用{{}} 非常相似於JSP,所以用起來感覺非常親切,非常順手
{{–><%
}}–>%>
比如:
<div id="evaluationtmpl">
{{ for(var prop in it) { }}
<div>KEY:{{= prop }}---VALUE:{{= it[prop] }}</div>
{{ } }}
</div>
是不是非常像:
<div id ="evaluationtmpl">
<% for(var prop in it) { %>
<div>KEY:{{= prop }}---VALUE:{{= it[prop] }}</div>
<% } %>
</div>
以下是一個二重循環的實例:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>客戶列表</title>
<link href="css/dealer.css" rel="stylesheet" type="text/css">
<link href="css/tcal.css" rel="stylesheet" type="text/css">
<script src="js/tcal.js" type="text/javascript"></script>
<script id="cuslist" type="text/x-dot-template">
<table border="1" >
{{ for(var prop in it){ }}
{{? typeof it[prop][‘appName‘]==‘object‘ }}
<tr >
<td rowspan="{{=it[prop][‘appName‘].length}}" >{{=it[prop][‘cusName‘]}}</td>
<td>{{=it[prop][‘appName‘][0]}}</td>
<td>{{=it[prop][‘appTime‘][0]}}</td>
</tr>
{{ for(var prop2=1;prop2 <it[prop][‘appName‘].length; prop2++ ){ }}
<tr>
<td >{{=it[prop][‘appName‘][prop2]}}</td>
<td >{{=it[prop][‘appTime‘][prop2]}}</td>
</tr>
{{ } }}
{{?? }}
<tr>
<td >{{=it[prop][‘cusName‘]}}</td>
<td >{{=it[prop][‘appName‘]}}</td>
<td >{{=it[prop][‘appTime‘]}}</td>
</tr>
{{?}}
{{ } }}
</tableb>
</script>
<script type="text/javascript" src="js/doT.min.js"></script>
</head>
<body>
<div id="table-list">
</div>
</body>
<script type="text/javascript">
var cusDate=[
{"cusName":"天X信息技術有限公司","appName":["好管家","朋友圈"],"appTime":["2015-07-12","2015-07-12"],"appPeop":["張三","李連清"]},
{"cusName":"天X信息公司","appName":["好管家","朋友圈"],"appTime":["2015-07-12","2015-07-23"],"appPeop":["張三","李連清"]},
{"cusName":"天X信息技公司","appName":"好管家","appTime":"2015-07-12","appPeop":"張青"},
{"cusName":"天X公司","appName":"好管家,a+","appTime":"2015-07-12","appPeop":"張青"},
];
//tmpljson={"testmode":"<td class=‘tab-com‘>{{=trValue.cusName}}</td><td class=‘tab-other‘>{{=trValue.appName}}</td><td class=‘tab-other‘>{{=trValue.appTime}}</td><td class=‘tab-name‘>aaa{{=trValue.appPeop}}</td>"}
// console.log(document.getElementById("cuslist").innerHTML);
console.log(‘----------------------‘);
var cusHtml=doT.template(document.getElementById("cuslist").innerHTML);
document.getElementById(‘table-list‘).innerHTML=cusHtml(cusDate);
/*document.getElementById(‘table-list‘).innerHTML= doT.template(cusHtml,undefined)(cusDate);*/
</script>
</html>
執行結果:
註意:
1,第二次循環的index是從1開始的,而不是從0開始;
2,doT JS的if推斷有兩種寫法:
寫法一:
<body>
<div id="conditionstmpl">
{{? !it.name }}
<div> 你還沒有名字</div>
{{?? }}
<div>Oh, I love your name, {{=it.name}}!</div>
{{?}}
</div>
<hr/>
<div id="condition"></div>
<script type="text/javascript">
var dataEncode = {"name": "黃威", "age": 31};
var interText = doT.template($("#conditionstmpl").html());
$("#condition").html(interText(dataEncode));
</script>
</body>
寫法二:
<body>
<div id="conditionstmpl">
{{ if(!it.name){ }}
<div> 你還沒有名字</div>
{{ } else { }}
<div>Oh, I love your name, {{=it.name}}!</div>
{{ } }}
</div>
<hr/>
<div id="condition"></div>
<script type="text/javascript">
var dataEncode = {"name": "", "age": 31};
var interText = doT.template($("#conditionstmpl").html());
$("#condition").html(interText(dataEncode));
</script>
</body>
參考:
doT js模板入門
doT js模板入門 2