jQuery-tmpl

jQuery-tmpl模板引擎

下载地址

  1. jquery.tmpl的几种常用标签分别有:

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

  2. 不常用标签

    {{=}},{{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>
  1. $.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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向阳,像样。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值