jquery.tmpl 用法(附上详细案例)

js的模板引擎就和服务端的差不多,都是更好更快的拼接html用于显示,我参考了文章:http://www.cnblogs.com/zhuzhiyuan/p/3510175.html

tmpl常用标签

${}{{each}}{{if}}{{else}}{{html}}

不常用标签

{{=}},{{tmpl}} and {{wrap}}.

1.

${}等同与{{=}}是输出变量 ${}里面还可以放表达式 (使用{{=}}的时候=和变量之间一定要有空格,否则无效,即为:{{= 变量}}

<div id="div_demo">
</div>
<script id="demo" type="text/x-jquery-tmpl">
<div >
  <span>${ID}</span>
  <span style="margin-left:10px;">{{= ID}}</span>
</div>
</script>
<script type="text/javascript">
  var users = [{ ID: ''}];
  $("#demo").tmpl(users).appendTo('#div_demo');
</script>

2.

{{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+}:{{= user.name}}</div>
{{if i==}}
<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>

该标签{{each}}   {{/each}}需要两两配合,其实就是each迭代器的用法{{each(i,data) datas}}  i是序号,data是单个数据,datas是数据集合,两这中间要有个空格

3.

{{if }} {{else}}提供了分支逻辑相当于else if,参照js语法即可

<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: , App: }, { ID: 'aCloud', Name: 'Mary Cheung', App: }, { ID: 'bMingdao', Name: 'Jerry Jin'}];
$("#ifelse").tmpl(users).appendTo('#div_ifelse');
</script>

4.
{{html}} 输出变量html,但是没有html编码,适合输出html代码

<div id="div_html"></div>
<script id="html" type="text/x-jquery-tmpl">
<div style="margin-bottom:10px;">
  <div>${html}</div>
  <div>{{html html}}</div>
</div>
</script>
<script type="text/javascript">
  var user = { html: '<button>html</button>' };
 $("#html").tmpl(user).appendTo('#div_html');
</script>

使用此字段注意{{html 变量}}中间也是一个空格

5.

{{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(Arr) '#tmpl2'}}</span>
</div>
</script>
<script id="tmpl2" type="type/x-jquery-tmpl">
{{each(i,item) List}}
${item.Name}
{{/each}}
</script>
<script type="text/javascript">
  var users = { ID: 'think8848', Arr: [ID:'',List: [{Name:'Joseph'},{ Name:'Chan'}]] };
$("#tmpl1").tmpl(users).appendTo('#tmpl');
</script>

同理{{tmpl(数据源) '模板选择器'}}

6.

{{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="" cellpadding="" border=""><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>

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>

8.
 $.tmplItem()方法,使用这个方法,可以获取从render出来的元素上重新获取$item,即从渲染的html中拿出数据

<script type="text/javascript">
  $('#demo').delegate('div', 'click', function () {
var item = $.tmplItem(this);
alert(item.data.Name);
});
</script>

9.

$.template函数

    <script type="text/x-jquery-tmpl" id="tmpl01">
        <h1>{{= name}}</h1>
        <h1>${id}</h1>
        <h1>${$item.getName(" ")}</h1>
        <h1>${$data.id}</h1>
    </script>
<script type="text/javascript">
$(function () {
var data = { id: '', name: '' };
//template函数可以定义模板
$.template("movieTemplate", $('#tmpl01'));
$.tmpl('movieTemplate', data).appendTo($('#test02'));
})
</script>
<div id="test02"></div>
上一篇:Linux 5.16 稳定版将带来诸多功能更新


下一篇:JAVA Io 缓冲输入输出流