動態請求資料來更新頁面是現在非常常用的方法,比如部落格評論的分頁動態載入,微博的滾動載入和定時請求載入等。
這些情況下,動態請求返回的資料一般不是已拼好的 HTML 就是 JSON 或 XML,總之不在瀏覽器端拼資料就在伺服器端拼資料。不過,從傳輸量方面來看,返回 HTML 不划算,而在 web 傳輸方面,現在更多的是使用 JSON 而不是 XML。
瀏覽器端根據 JSON 生成 HTML 有個很苦惱的地方就是,結構不復雜的時候還好,結構一複雜,就想死了,需要很小心很小心地寫出幾乎無法維護的 JavaScript 程式碼。
因此一些用模版生成HTML的的框架相繼出現jquery.tmpl 就是其中的一種,下面我們來詳細介紹下jquery.tmpl的用法
jquery.tmpl的幾種常用標籤分別有:
${}, {{each}}, {{if}}, {{else}}, {{html}}
不常用標籤
{{=}},{{tmpl}} and {{wrap}}.
${}等同與{{=}}是輸出變數 ${}裡面還可以放表示式 (=和變數之間一定要有空格,否則無效)
示例:
<div id="div_demo"> </div> <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)
示例:
<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
示例:
<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程式碼
例項
<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}} 巢狀模版
例項
<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>
{{wrap}},包裝器
例項
<div id="wrapDemo"> </div> <script id="myTmpl" type="text/x-jquery-tmpl"> The following wraps and reorders some HTML content: {{wrap "#tableWrapper"}} <h3>One</h3> <div> First <b>content</b> </div> <h3>Two</h3> <div> And <em>more</em> <b>content</b>... </div> {{/wrap}} </script> <script id="tableWrapper" type="text/x-jquery-tmpl"> <table cellspacing="0" cellpadding="3" border="1"><tbody> <tr> {{each $item.html("h3", true)}} <td> ${$value} </td> {{/each}} </tr> <tr> {{each $item.html("div")}} <td> {{html $value}} </td> {{/each}} </tr> </tbody></table> </script> <script type="text/javascript"> $(function () { $('#myTmpl').tmpl().appendTo('#wrapDemo'); }); </script>
$data $item $item代表當前的模板;$data代表當前的資料。
例項:
<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
例項
<script type="text/javascript"> $('#demo').delegate('div', 'click', function () { var item = $.tmplItem(this); alert(item.data.Name); }); </script>
how to tell if a property exists and is false
{{if Value}}
Value exists and is true
{{else typeof(Value) != "undefined" && Value === false}}
Value exists and is false
{{else}}
Value doesn't exist or isn't explicitly false
{{/if}}
獲取索引:
$("#templateToRender").tmpl(jsonData,
{
getIndex: function (item) {
return $.inArray(item, jsonData);
}
});
<script id="templateToRender" type="text/x-jquery-tmpl">
<li>
索引: ${$item.getIndex($item.data)}
</li>
</script>