jquery tmpl使用(EL表达式冲突解决)
转载:http://www.cnblogs.com/zhuzhiyuan/p/3510175.html
1.jquery.tmpl用法
jquery.tmpl的几种常用标签分别有:
${}, {{each}}, {{if}}, {{else}}, {{html}}
不常用标签
{{=}},{{tmpl}} and {{wrap}}.
${}等同与{{=}}是输出变量 ${}里面还可以放表达式 (=和变量之间一定要有空格,否则无效)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <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代码
1 2 3 4 5 6 7 8 9 10 11 12 13 | <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}} 嵌套模版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <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}},包装器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <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代表当前的数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <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
1 2 3 4 5 6 | <script type="text/javascript"> $('#demo').delegate('div', 'click', function () { var item = $.tmplItem(this); alert(item.data.Name); }); </script> |
2.与EL表达式冲突
${name}这种取值方式会与JSP的el表达式冲突,导致不能正常取值,可以通过以下三种方式解决:
1.在“$”前加“\”,如“\${name}”,推荐方式;
2.用{{= name}},缺点是不能使用表达式;
3.jsp中加<%@page isELIgnored="true" %>,让EL表达式失效,当页面没有使用到el时,可以这样用,否则还是用方式1比较好。
Comments are currently closed.