1、为什么要使用tmpl?
浏览器端根据 JSON 生成 HTML 有个很苦恼的地方就是,结构不复杂的时候还好,结构一复杂,就想死了,需要很小心很小心地写出几乎无法维护的 JavaScript 代码。
2、tmpl具体应用的方面?
动态请求数据来更新页面是现在非常常用的方法,比如博客评论的分页动态加载,微博的滚动加载和定时请求加载、相册时光轴的加载等。这些情况下,动态请求返回的数据一般不是已拼好的 HTML 就是 JSON 或 XML,总之不在浏览器端拼数据就在服务器端拼数据。不过,从传输量方面来看,返回 HTML 不划算,而在 web 传输方面,现在更多的是使用 JSON 而不是 XML。所以说,一般在浏览器端拼数据比较不错。
3、如何使用?
首先得引入jquery.tmpl.js这个包,否则无法使用。
4、jquery tmpl中一些标签的使用
${}等同与{{=}}是输出变量 ${}里面还可以放表达式 (=和变量之间一定要有空格,否则无效)
示例1:
<div id="div_demo">
</div>
/*!注意这里的type="text/x-jquery-tmpl"*/
<script id="demo" type="text/x-jquery-tmpl">
<div style="margin-bottom:10px;">
<span>${ID}</span>
<span style="margin-left:10px;">{{= Name}}</span>
<span style="margin-left:10px;">${Number(Num)+1}</span>
<span style="margin-left:10px;">${Status}</span>
</div>
</script>
<script type="text/javascript">
var users = [{ ID: 'think8848', Name: 'Joseph Chan', Num: '1', Status: 1 }, { ID: 'aCloud', Name: 'Mary Cheung', Num: '2'}];
$("#demo").tmpl(users).appendTo('#div_demo');
</script>
{{each}} 提供循环逻辑,$value访问迭代变量 也可以自定义迭代变量(i,value)
示例2:
<div id="div_each">
</div>
<script id="each" type="text/x-jquery-tmpl">
<h3>users</h3>
{{each(i,user) users}}
<div>${i+1}:{{= user.name}}</div>
{{if i==0}}
<h4>group</h4>
{{each(j,group) groups}}
<div>${group.name}</div>
{{/each}}
{{/if}}
{{/each}}
<h3>depart</h3>
{{each departs}}
<div>{{= $value.name}}</div>
{{/each}}
</script>
<script type="text/javascript">
var eachData = { users: [{ name: 'jerry' }, { name: 'john'}], groups: [{ name: 'mingdao' }, { name: 'meihua' }, { name: 'test'}], departs: [{ name: 'IT'}] };
$("#each").tmpl(eachData).appendTo('#div_each');
</script>
{{if }} {{else}}提供了分支逻辑 {{else}} 相当于else if
示例3:
<div id="div_ifelse"></div>
<script id="ifelse" type="text/x-jquery-tmpl">
<div style="margin-bottom:10px;"><span>${ID}</span><span style="margin-left:10px;">{{= Name}}</span>
{{if Status}}
<span>Status${Status}</span>
{{else App}}
<span>App${App}</span>
{{else}}
<span>None</span>
{{/if}}
</div>
</script>
<script type="text/javascript">
var users = [{ ID: 'think8848', Name: 'Joseph Chan', Status: 1, App: 0 }, { ID: 'aCloud', Name: 'Mary Cheung', App: 1 }, { ID: 'bMingdao', Name: 'Jerry Jin'}];
$("#ifelse").tmpl(users).appendTo('#div_ifelse');
</script>
{{html}} 输出变量html,但是没有html编码,适合输出html代码
示例4:
<div id="div_html"></div>
<script id="html" type="text/x-jquery-tmpl">
<div style="margin-bottom:10px;">
<span>${ID}</span>
<span style="margin-left:10px;">{{= Name}}</span>
${html}
{{html html}}
</div>
</script>
<script type="text/javascript">
var user = { ID: 'think8848', Name: 'Joseph Chan', html: '<button>html</button>' };
$("#html").tmpl(user).appendTo('#div_html');
</script>
{{tmpl}} 嵌套模版
示例5:
<div id="tmpl"></div>
<script id="tmpl1" type="text/x-jquery-tmpl">
<div style="margin-bottom:10px;">
<span>${ID}</span>
<span style="margin-left:10px;">{{tmpl($data) '#tmpl2'}}</span>
</div>
</script>
<script id="tmpl2" type="type/x-jquery-tmpl">
{{each Name}}${$value} {{/each}}
</script>
<script type="text/javascript">
var users = [{ ID: 'think8848', Name: ['Joseph', 'Chan'] }, { ID: 'aCloud', Name: ['Mary', 'Cheung']}];
$("#tmpl1").tmpl(users).appendTo('#tmpl');
</script>
$data $item $item代表当前的模板;$data代表当前的数据。
示例6:
<div id="div_item_data"></div>
<script id="item_data" type="text/x-jquery-tmpl">
<div style="margin-bottom:10px;">
<span>${$data.ID}</span>
<span style="margin-left:10px;">${$item.getName(" ")}</span>
</div>
</script>
<script type="text/javascript">
var users = [{ ID: 'think8848', Name: ['Joseph', 'Chan'] }, { ID: 'aCloud', Name: ['Mary', 'Cheung']}];
$("#item_data").tmpl(users,
{
getName: function (spr) {
return this.data.Name.join(spr);
}
}).appendTo('#div_item_data');
</script>
$.tmplItem()方法,使用这个方法,可以获取从render出来的元素上重新获取$item
示例7:
<script type="text/javascript">
/*!.delegate() 指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数。*/
$('#demo').delegate('div', 'click', function () {
var item = $.tmplItem(this);
alert(item.data.Name);
});
</script>
$template的使用:
示例8:
/*!$.template()方法,将一段Html编译为模板*/
var markup = '<tr><td>${ID}</td><td>${Name}</td></tr>';
$.template('template', markup);
$.tmpl('template', users).appendTo('#templateRows');
5、自己代码举例
<div id = "phototimelime" class="content"></div>
</div>
<script id="each" type="text/x-jquery-tmpl">
<ul>
{{each(i,item) $data.list}}
<li> <span class="point-time point-{{= i%5}}"></span>
<time {{if item.TEST_TIME != ""}}id="{{= item.TEST_TIME}}"{{/if}} datetime="{{= item.CREATE_TIME}}">{{= item.SHOW_TIME}}</time>
<dl>
<dt>
照片信息 <p class="things text-{{= i%5}}">照片名字:{{= item.NAME}}</p>
<p class="brief"><span class="text-yellow">上传时间:</span>{{= item.CREATE_TIME}}</p>
</dt>
<dd><div class="photo_info">
<a class="abc" href="http://localhost:8080/rc/album/photodetail.htm?pid={{= item.PHOTO_ID}}" class="bot">
<img alt="示例图片_04" id="1827" src="http://localhost:8088/RCService/image?photo_id={{= item.PHOTO_ID}}" width="180px" height="180px">
</a></div>
</dd>
</dl>
</li>
{{/each}}
</ul>
</script>
<script type="text/javascript">
$(document).ready(function(){
var showCount = 5;
var currentYear = new Date().toLocaleString().substring(0,4);
/*初始化创建相册时间监听*/
initPhotoTimeLine(showCount,currentYear);
});
/*初始化时光轴*/
function initPhotoTimeLine(count,currentYear){
var url = CONST.SERVICE_URL+"/album/timeLine.do?method=getPhotoInfo&count="+count+"¤tYear="+currentYear;
$.post(url,function(data){
debugger;
if(data != null&&data!='[]'&&data != ""){
data = JSON.parse(data);
/*设计月份*/
var month = new Array("","一月(Jan)","二月(Feb)","三月(Mar)","四月(Apr)","五月(May)","六月(Jun)","七月(Jul)","八月(Aug)","九月(Sep)"
,"十月(Oct)","十一月(Nov)","十二月(Dec)");
var month_type = new Array("","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep"
,"Oct","Nov","Dec");
var flag_month = "月份";
$.each(data,function(k,v){
if(v.SHOW_TIME.substring(7,5) != flag_month){
debugger;
flag_month = v.SHOW_TIME.substring(7,5);
v.TEST_TIME = month_type[removeZero(v.SHOW_TIME.substring(7,5))];
v.SHOW_TIME = month[removeZero(v.SHOW_TIME.substring(7,5))];
}else{
v.SHOW_TIME = "";
v.TEST_TIME = "";
}
//alert(v.SHOW_TIME);
});
$("#phototimelime").html('');
$("#each").tmpl({"list":data}).appendTo('#phototimelime');
}else{
$("#phototimelime").html('<div style="text-align:center;padding:6px;font-size:16px;color:#999;">暂无数据</div>');
$(".right").css("display","none");
}
});
}
/*除去字符串中0的方法*/
function removeZero(str){
return str.substring(0,1) == "0"?str.substring(1):str;
}
</script>