<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dot模板总结</title>
</head>
<body>
<div id="div1"> </div>
<!--安装dot模板-->
<script src="dot.js"></script> <!--dot的javascript模板-->
<script id="j-tmpl" type="text/template">
{{ if(it.success){ }}
<h2>results:</h2>
<ul>
{{ for (var i = 0, l = it.data.length; i < l; i++) { }}
<li>
<h5>{{=it.data[i].title}}</h5>
<p>{{=it.data[i].message}}</p>
</li>
{{ } }}
<ul>
{{ }else{ }}
<h2>暂无数据</h2>
{{ } }}
</script> <!--1.获取数据,2.填充数据到模板,3.将填充数据后的模板填充的html代码指定位置-->
<script>
//伪造数据
var obj = {
success: true,
data:[
{title:'item1',message:11},
{title:'item2',message:22}
]
}
//初始化模板
var tmpl = document.getElementById('j-tmpl').innerHTML;
var doTtmpl = doT.template(tmpl);
//模板填充数据
var data = doTtmpl(obj );
//模板填充到指定位置
var dd = document.getElementById("div1");
dd.innerHTML = data;
</script>
</body>
</html>
相关博文:http://www.fantxi.com/blog/archives/dot-template/
http://www.cnblogs.com/kuikui/p/3505768.html
dot文档:http://olado.github.io/doT/index.html