Dynamic requesting data to update the page is a very common method nowadays, such as paging dynamic loading of blog comments, rolling loading and scheduled request loading of Weibo, etc.
In these cases, the data returned by the dynamic request is usually either assembled HTML, JSON or XML. In short, the data is not assembled on the browser side, but on the server side. However, returning HTML is not cost-effective in terms of transmission volume, and in terms of web transmission, JSON is now used more than XML.
A very troublesome part of generating HTML based on JSON on the browser side is that it's okay when the structure is not complicated, but once the structure is complicated, it becomes a nightmare. You need to be very careful to write JavaScript code that is almost impossible to maintain.
Therefore, some frameworks that use templates to generate HTML have appeared one after another. jquery.tmpl is one of them. Let’s introduce the usage of jquery.tmpl in detail
The following will focus on the usage:
First, let’s introduce the template and data , Needless to say, these two are definitely indispensable. There are two ways to define templates. The specific codes are given below
var markup = "<li>Some content: ${item}.<br/>" + " More content: ${myValue}.</li>"; $.template( "movieTemplate", markup );
<script id="movieTemplate" type="text/x-jquery-tmpl"> <li><b>${Name}</b> (${ReleaseYear})</li> </script>
In this way, two templates are defined. The former one is written in script, and the latter one is written in html. The
data inside is json
Start rendering the template below
$( "#movieTemplate" ).tmpl( movies ).appendTo( "#movieList" ); $.tmpl( "movieTemplate", movies ).appendTo( "#movieList" );
Note: movies is a json data array
var movies = [ { Name: "The Red Violin", ReleaseYear: "1998" }, { Name: "Eyes Wide Shut", ReleaseYear: "1999" }, { Name: "The Inheritance", ReleaseYear: "1976" } ];
The common tags of jquery.tmpl are:
${}, {{each}}, {{ if}}, {{else}}, {{html}}
Uncommon tags
{{=}},{{tmpl}} and {{wrap}}.
${} is equivalent to {{= }} is the output variable ${} and you can also put expressions in it (there must be a space between = and the variable, otherwise it will be invalid)
Example:
<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}} provides loop logic, and $value also accesses iteration variables You can customize the iteration variable (i, value)
Example:
<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}} provides branch logic {{else}} which is equivalent to else if
Example:
<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 }} Output variable html, but without html encoding, suitable for outputting html code
instance
<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}} nested template
instance
<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}}, wrapper
instance
<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 represents the current template; $data represents the current data.
Example:
<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() method, using this method, you can get $item from the rendered element
Instance
<script type="text/javascript"> $('#demo').delegate('div', 'click', function () { var item = $.tmplItem(this); alert(item.data.Name); }); </script>
More frameworks for generating HTML using templates Please pay attention to the PHP Chinese website for related articles about the use of jquery.tmpl!