jquery.tmpl的几种常用标签分别有:
${}, {{each}}, {{if}}, {{else}}, {{html}}
不常用标签
{{=}},{{tmpl}} and {{wrap}}.
${}等同与{{=}}是输出变量 ${}里面还可以放表达式 (=和变量之间一定要有空格,否则无效)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="./js/jquery.js"></script>
<script src="./js/jquery-tmpl.js"></script>
<title>测试jQuery-tmpl的使用</title>
</head>
<body>
<div id="div_demo"></div>
</body>
<!-- 模板1,测试${}、{{=}}标签的使用 -->
<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;">${age}</span>
<span style="margin-left:10px;">${number}</span>
</div>
</script>
<script type="text/javascript">
//手动初始化数据
var users = [{ id: 1, name: 'xiaoming', age: 12, number: '001' }, { id: 2, name: 'xiaowang', age: 13, number: '002' }];
//调用模板进行渲染
$("#demo").tmpl(users).appendTo('#div_demo');
</script>
</html>
3.{{each}} 提供循环逻辑,$value访问迭代变量 也可以自定义迭代变量(i,value)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="./js/jquery.js"></script>
<script src="./js/jquery-tmpl.js"></script>
<title>测试{{each}}的使用</title>
</head>
<body>
<div id="div_each"></div>
</body>
<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>
</html>
4.**{{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>
5.{{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>
6.{{wrap}},包装器
实例
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="./js/jquery.js"></script>
<script src="./js/jquery-tmpl.js"></script>
<title>测试{{wrap}}的使用</title>
</head>
<body>
<div id="div_each"></div>
</body>
<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>
</html>
7.$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
实例
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="./js/jquery.js"></script>
<script src="./js/jquery-tmpl.js"></script>
<title>测试jQuery-tmpl的使用</title>
</head>
<body>
<div id="div_each"></div>
</body>
<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');
$('#div_item_data').delegate('div', 'click', function () {
var item = $.tmplItem(this);
alert(JSON.stringify(item));
});
</script>
</html>