官方解释对该插件的说明:将匹配的第一个元素做为模板,render指定的数据,签名以下:javascript
.tmpl([data,][options])
|
其中参数data的用途很明显:用于render的数据,能够是任意js类型,包括数组和对象。options通常状况下都是选项了,官方指出,此处的options是一个用户自定义的键值对的map,继承自tmplItem数据结构,适用于模板render动做期间。css
好吧,先来一个最直观的例子:html
<%@ Page Language="C#" AutoEventWireup="true" %>
<!
DOCTYPE
html>
<
html
>
<
head
>
<
title
>jquery template demo</
title
>
<
link
rel="stylesheet" href="content/site.css" type="text/css" />
<
link
rel="stylesheet" href="content/jquery.ui.css" type="text/css" />
<
script
type="text/javascript" src="scripts/jquery.js"></
script
>
<
script
type="text/javascript" src="scripts/jquery.ui.js"></
script
>
<
script
type="text/javascript" src="scripts/jquery.tmpl.js"></
script
>
<
script
id="myTemplate" type="text/x-jquery-tmpl">
<
tr
><
td
>${ID}</
td
><
td
>${Name}</
td
></
tr
>
</
script
>
<
script
type="text/javascript">
$(function () {
var users = [{ ID: 'think8848', Name: 'Joseph Chan' }, { ID: 'aCloud', Name: 'Mary Cheung'}];
$('#myTemplate').tmpl(users).appendTo('#rows');
});
</
script
>
<
style
type="text/css">
body
{
padding: 10px;
}
table
{
border-collapse: collapse;
}
</
style
>
</
head
>
<
body
>
<
table
cellspacing="0" cellpadding="3" border="1">
<
tbody
id="rows">
</
tbody
>
</
table
>
</
body
>
</
html
>
|
例子虽然很小也很简单,但我以为这个已经颇有用了。java
固然,.tmpl()还可使用来自远端的数据,好比说服务:jquery
public
ActionResult SampleData()
{
var
json =
new
JsonResult();
json.Data =
new
[] {
new
{ ID =
"remote1"
, Name =
"abcd"
},
new
{ ID =
"remote2"
, Name =
"efg"
} };
json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return
json;
}
|
这是一个MVC的Action,我把它当作是一个提供数据的服务,而后js代码以下:express
$(
'#btnAjax'
).click(
function
() {
$.getJSON(
'@Url.Action("SampleData", "Home")'
,
function
(json) {
$(
'#rows'
).empty();
$(
'#myTemplate'
).tmpl(json).appendTo(
'#rows'
);
});
});
|
效果:json
定义模板时,推荐的方式为定义使用数组
<script id=
'templateName'
type=
'text/x-jquery-tmpl'
></script>
|
作为模板的包装器,但定义方式并不仅有这一种,你可使用浏览器
<
div
id="template" style="display: none;">
<!-- markup -->
</
div
>
|
的方式来定义,可是官方文档中说,这种方法可能会产生浏览器没法解析的HTML,所以不推荐使用,不过我试了下,倒没有出什么意外:缓存
HTML:
<
div
id="container">
</
div
>
<
div
id="inline" style="display: none">
<
label
>
${ID}</
label
>
<
label
>
${Name}</
label
><
br
/>
</
div
>
|
Javascript:
var
users = [{ ID:
'think8848'
, Name:
'Joseph Chan'
}, { ID:
'aCloud'
, Name:
'Mary Cheung'
}];
$(
'#inline'
).tmpl(users).appendTo(
'#container'
);
|
效果:
编译缓存模板,在jQuery .tmpl()中,还能够将模板事先编译并缓存起来,而后在合适的时侯再使用,这对于一些数据嵌套是颇有用的,如:
Html
<
table
cellspacing="0" cellpadding="3" border="1">
<
tbody
id="compileRows">
</
tbody
>
</
table
>
|
Javascript
<script id=
"compile1"
type=
"text/x-jquery-tmpl"
>
{{tmpl
'cached'
}}
<tr><td>${ID}</td><td>${Name}</td></tr>
</script>
<script id=
"compile2"
type=
"type/x-jquery-tmpl"
>
<tr><td colspan=
"2"
>${Group}</td></tr>
</script>
<script type=
"text/javascript"
>
$(
function
() {
var
groupUsers = [{ ID:
'think8848'
, Name:
'Joseph Chan'
, Group:
'Administrators'
}, { ID:
'aCloud'
, Name:
'Mary Cheung'
, Group:
'Users'
}];
$(
'#compile2'
).template(
'cached'
);
$(
'#compile1'
).tmpl(groupUsers).appendTo(
'#compileRows'
);
});
</script>
|
效果:
$.template()方法,将一段Html编译为模板,示例:
Javascript
var
markup =
'<tr><td>${ID}</td><td>${Name}</td></tr>'
;
$.template(
'template'
, markup);
$.tmpl(
'template'
, users).appendTo(
'#templateRows'
);
|
这样就能够将markup中定义的模板应用于templateRows对象。
jQuery .tmpl()的标签,表达式,属性:
${}:从前面的例子来看,这个标签的做用很明显了,至关因而占位符,可是它还有另外一种写法{{= field}}如:
<
script
id="myTemplate" type="text/x-jquery-tmpl">
<
tr
><
td
>{{= ID}}</
td
><
td
>{{= Name}}</
td
></
tr
>
</
script
>
|
必需要注意的是,"="号后必须跟一个空格,否则是没有效果的。
另外,${}中还能够放表达式,这个牛x吧,如:
Html
<
table
cellspacing="0" cellpadding="3" border="1">
<
tbody
id="expressionRows">
</
tbody
>
</
table
>
|
Javascript
<script type=
"text/javascript"
>
$(
function
() {
var
userLangs = [{ ID:
'think8848'
, Name:
'Joseph Chan'
, Langs: [
'Chinese'
,
'English'
] }, { ID:
'aCloud'
, Name:
'Mary Cheung'
, Langs: [
'Chinese'
,
'French'
]}];
$(
'#expression'
).tmpl(userLangs).appendTo(
'#expressionRows'
);
});
</script>
|
效果:
jQuery .tmpl()有两个比较有用的属性:$item、$data:
$item表明当前的模板;$data表明当前的数据。
Html
<
table
cellspacing="0" cellpadding="3" border="1">
<
tbody
id="propertyRows">
</
tbody
>
</
table
>
|
Javascript
<script id=
"property"
type=
"text/x-jquery-tmpl"
>
<tr><td>${ID}</td><td>${$data.Name}</td><td>${$item.getLangs(
'; '
)}</td></tr> </script>
<script type=
"text/javascript"
>
$(
function
() {
var
userLangs = [{ ID:
'think8848'
, Name:
'Joseph Chan'
, Langs: [
'Chinese'
,
'English'
] }, { ID:
'aCloud'
, Name:
'Mary Cheung'
, Langs: [
'Chinese'
,
'French'
]}];
$(
'#property'
).tmpl(userLangs, {
getLangs:
function
(separator) {
return
this
.data.Langs.join(separator);
}
})
.appendTo(
'#propertyRows'
);
});
</script>
|
效果:
{{each}}这个标签一看就知道是作循环用的了,用法以下:
Html
<
ul
id="eachList">
</
ul
>
|
Javascript
<script id=
"each"
type=
"text/x-jquery-tmpl"
>
<li>ID: ${ID}; Name: ${Name};<br />Langs:<ul>{{each Langs}}<li>${$index + 1}: <label>${$value}. </label></li>{{/each}}<ul></li>
</script>
<script type=
"text/javascript"
>
$(
function
() {
var
userLangs = [{ ID:
'think8848'
, Name:
'Joseph Chan'
, Langs: [
'Chinese'
,
'English'
] }, { ID:
'aCloud'
, Name:
'Mary Cheung'
, Langs: [
'Chinese'
,
'French'
]}];
$(
'#each'
).tmpl(userLangs).appendTo(
'#eachList'
);
});
|
效果:
{{each}}还有另外一种写法:
Javascript
<script id=
"each2"
type=
"text/x-jquery-tmpl"
>
<li>ID: ${ID}; Name: ${Name};<br />Langs:<ul><strong>{{each(i,lang) Langs}}<li>${i + 1}: <label>${lang}. </label></li>{{/each}}</strong></ul></li>
</script>
|
做用和前一种是同样的。
{{if}}和{{else}},这两个标签应该一看就知道做用了,直接上示例:
Javascript
<script id=
"ifelse"
type=
"text/x-jquery-tmpl"
>
<tr><td>${ID}</td><td>${Name}</td><td>{{
if
Langs.length > 1}}${Langs.join(
'; '
)}{{
else
}}${Langs}{{/
if
}}</td></tr>
</script>
|
若是Langs数组元素超过1个,则用'; '链接起来,不然就直接显示Langs,效果:
{{html}},直接将对象属性值做为HTML代码替换占位符:
Javascript
<script id=
"html"
type=
"text/x-jquery-tmpl"
>
<tr><td>${ID}</td><td>${Name}</td><td>{{html Ctrl}}</td></tr>
</script>
<script type=
"text/javascript"
>
$(
function
() {
var
ctrls = [{ ID:
'think8848'
, Name:
'Joseph Chan'
, Ctrl:
'<input type="button" value="Demo" />'
}, { ID:
'aCloud'
, Name:
'Mary Cheung'
, Ctrl:
'<input type="text" value="Demo" />'
}];
$(
'#html'
).tmpl(ctrls).appendTo(
'#htmlRows'
);
});
</script>
|
效果:
{{tmpl}},前面已经提过该标签了,这里再给一个使用参数的例子:
Javascript
<script id=
"tmpl1"
type=
"text/x-jquery-tmpl"
>
<tr><td>${ID}</td><td>${Name}</td><td> {{tmpl($data)
'#tmpl2'
}}</td></tr>
</script>
<script id=
"tmpl2"
type=
"type/x-jquery-tmpl"
>
{{each Langs}}${$value} {{/each}}
</script>
<script type=
"text/javascript"
>
$(
function
() {
var
userLangs = [{ ID:
'think8848'
, Name:
'Joseph Chan'
, Langs: [
'Chinese'
,
'English'
] }, { ID:
'aCloud'
, Name:
'Mary Cheung'
, Langs: [
'Chinese'
,
'French'
]}];
$(
'#tmpl1'
).tmpl(userLangs).appendTo(
'#tmplRows'
);
});
</script>
|
效果:
{{wrap}},包装器,这回不须要指定对哪个元素使用模板了,直接生成模板的包装器,示例:
Html
<
div
id="wrapDemo">
</
div
>
|
Javascript
<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>
|
效果:
$.tmplItem()方法,使用这个方法,能够获取从render出来的元素上从新获取$item,示例:
$(
'tbody'
).delegate(
'tr'
,
'click'
,
function
() {
var
item = $.tmplItem(
this
);
alert(item.data.Name);
});
|
效果:
文章做者:cat qi 文章出处:https://www.cnblogs.com/qixuejia/p/5188505.html