引入文件
<script src="../js/laytpl.js"></script>
//第一步:編寫模版。你可以使用一個script標(biāo)簽存放模板,如:
<script id="demo" type="text/html">
<h3>{{ d.title }}</h3>
<ul>
{{# layui.each(d.list, function(index, item){ }}
<li>
<span>{{ item.modname }}</span>
<span>{{ item.alias }}:</span>
<span>{{ item.site || '' }}</span>
</li>
{{# }); }}
{{# if(d.list.length === 0){ }}
無數(shù)據(jù)
{{# } }}
</ul>
</script>
//第二步:建立視圖。用于呈現(xiàn)渲染結(jié)果。
<div id="view"></div>
//第三步:渲染模版
var data = { //數(shù)據(jù)
"title":"Layui常用模塊"
,"list":[{"modname":"彈層","alias":"layer","site":"layer.layui.com"},{"modname":"表單","alias":"form"}]
}
var getTpl = demo.innerHTML
,view = document.getElementById('view');
laytpl(getTpl).render(data, function(html){
view.innerHTML = html;
});