jQuery .tmpl() 用法示例介紹
動態(tài)請求數(shù)據(jù)來更新頁面是現(xiàn)在非常常用的方法,比如博客評論的分頁動態(tài)加載,微博的滾動加載和定時(shí)請求加載等。
這些情況下,動態(tài)請求返回的數(shù)據(jù)一般不是已拼好的 HTML 就是 JSON 或 XML,總之不在瀏覽器端拼數(shù)據(jù)就在服務(wù)器端拼數(shù)據(jù)。不過,從傳輸量方面來看,返回 HTML 不劃算,而在 web 傳輸方面,現(xiàn)在更多的是使用 JSON 而不是 XML。
瀏覽器端根據(jù) JSON 生成 HTML 有個(gè)很苦惱的地方就是,結(jié)構(gòu)不復(fù)雜的時(shí)候還好,結(jié)構(gòu)一復(fù)雜,就想死了,需要很小心很小心地寫出幾乎無法維護(hù)的 JavaScript 代碼。
如同為解決 PHP 拼數(shù)據(jù)這方面的問題而有了 Smarty 這些模版,JavaScript 也可以利用模版來解決這些問題,比如基于 jQuery 的 jquery.tmpl,現(xiàn)在已經(jīng)被接受為官方的模版插件了。詳細(xì)的 API 在 jQuery 的 Templates 里,內(nèi)置的 demo 也盡情地演示了各種用法。
就我自己的幾次使用,感覺很不錯(cuò),用更加直觀方面的 HTML 寫法而不是 JavaScript 拼湊 來寫結(jié)構(gòu),然后用 JSON 變量來占位的方式來填充數(shù)據(jù),代碼看起來好多了。
Tmpl提供了幾種tag:
${}:等同于{{=}},是輸出變量,通過了html編碼的。
{{html}}:輸出變量html,但是沒有html編碼,適合輸出html代碼。
{{if }} {{else}}:提供了分支邏輯。
{{each}}:提供循環(huán)邏輯,$value訪問迭代變量。
jquery tmpl的使用方法:
模板定義:
方法一:
<script id="movieTemplate" type="text/x-jquery-tmpl">
<li>
<b>${Name}</b> (${ReleaseYear})
</li>
</script>
方法二:
function makeTemplate(){
var markup='<li><b>${Name}</b> (${ReleaseYear})</li>‘;
$.template(“movieTemplate”, markup);
}
DATA:
var movies = [
{ Name: "The Red Violin", ReleaseYear: "1998" },
{ Name: "Eyes Wide Shut", ReleaseYear: "1999" },
{ Name: "The Inheritance", ReleaseYear: "1976" }
];
Script:
$( "#movieTemplate" ).tmpl( movies ) .appendTo( "#movieList" );
實(shí)例1:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>
</head>
<body>
<ul class="param-list"></ul>
<script type="text/x-jquery-tmpl" id="new-param-tmpl">
<li rel="${num}">
<input type="text" name="key[${num}]" value="${key}" placeholder="key" /> =
<input type="text" name="value[${num}]" value="${value}" placeholder="value" />
<button type="button" class="button small remove-param"><img src="http://mediacdn.disqus.com/1338845651/images/v3/icon/16/69.png" height="12" alt=""/></button>
<button type="button" class="button small add-param"><span><img src="http://mediacdn.disqus.com/1338845651/images/v3/icon/16/13.png" height="12" alt=""/></button>
</li>
</script>
<script>
$(function(){
function addParam(key, value) {
var param_list = $('.param-list');
var num = param_list.find('li').length;
// build a template to clone the current row
var built = $('#new-param-tmpl').tmpl({
num: num,
key: key || '',
value: value || '',
});
if (key) param_list.prepend(built);
else param_list.append(built);
param_list.find('li:not(:last) .add-param').hide();
param_list.find('li:last .add-param').show();
param_list.find('li:not(:last) .remove-param').show();
param_list.find('li:last .remove-param').hide();
}
// bind events
$('.param-list .remove-param').live('click', function(){
$(this).parent().remove();
return false;
});
$('.param-list .add-param').live('click', function(){
addParam();
return false;
});
addParam();
})
</script>
</body>
</html>
實(shí)例2
<ul id="movieList"></ul>
<script id="movieTemplate" type="text/x-jquery-tmpl">
<li><b>${Name}</b> (${ReleaseYear})</li>
</script>
<script type="text/javascript">
var movies = [
{ Name: "The Red Violin", ReleaseYear: "1998" },
{ Name: "Eyes Wide Shut", ReleaseYear: "1999" },
{ Name: "The Inheritance", ReleaseYear: "1976" }
];
// Render the template with the movies data and insert
// the rendered HTML under the "movieList" element
$( "#movieTemplate" ).tmpl( movies )
.appendTo( "#movieList" );
</script>
相關(guān)文章
jQuery Migrate 1.1.0 Released 注意事項(xiàng)
jQuery開發(fā)團(tuán)隊(duì)前一段時(shí)間發(fā)布了jQuery 1.9版本,刪除了1.8版本中的部分API,為了使前端開發(fā)者能夠順利遷移至該版本,該團(tuán)隊(duì)還發(fā)布了遷移插件jQuery Migrate2014-06-06
JS Canvas定時(shí)器模擬動態(tài)加載動畫
這篇文章主要為大家詳細(xì)介紹了JS Canvas定時(shí)器模擬動態(tài)加載動畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
根據(jù)Bootstrap Paginator改寫的js分頁插件
本文主要對根據(jù)Bootstrap Paginator改寫的js插件進(jìn)行詳細(xì)介紹,具有很好的參考價(jià)值,需要的朋友一起來看下吧2016-12-12
jQuery中parentsUntil()方法用法實(shí)例
這篇文章主要介紹了jQuery中parentsUntil()方法用法,實(shí)例分析了parentsUntil()方法的功能、定義及根據(jù)條件查找匹配元素的所有的祖先元素使用技巧,需要的朋友可以參考下2015-01-01
jQuery中offsetParent()方法用法實(shí)例
這篇文章主要介紹了jQuery中offsetParent()方法用法,實(shí)例分析了offsetParent()方法的功能、定義及返回匹配元素所有祖先元素中第一個(gè)采用定位的祖先元素時(shí)的使用技巧,需要的朋友可以參考下2015-01-01
使用jQuery+HttpHandler+xml模擬一個(gè)三級聯(lián)動的例子
昨天同學(xué)問我有關(guān)如何快速讀取多層級xml文件的問題,于是我就使用省、市、縣模擬了一個(gè)三級聯(lián)動的例子,客戶端使用jQuery實(shí)現(xiàn)異步加載服務(wù)器返回的json數(shù)據(jù),服務(wù)器端則使用XPath表達(dá)式查詢數(shù)據(jù)。2011-08-08
EasyUI中combobox默認(rèn)值注意事項(xiàng)
這篇文章主要介紹了EasyUI中combobox默認(rèn)值注意事項(xiàng),是個(gè)人在項(xiàng)目中遇到并解決的事宜,分享給大家,需要的朋友可以參考下2015-03-03

