---
title: "jTemplates简介"
author: "陈先森"
date: "2014-07-21T11:13:42Z"
tags: []
categories: ["Web前端"]
slug: "jtemplates"
draft: false
lastmod: "2013-12-26T10:32:52Z"
id: 1995569
url: "https://zhanchan.iteye.com/blog/1995569"
---

jTemplates是一个基于JQuery的模板引擎插件，功能强大，有了他你就再不用为使用JS绑定数据集时发愁了。

首先送上jTtemplates的官网地址：http://jtemplates.tpython.com/，你可以在这里下载代码和相关参考文档

在使用jTemplates时最好先定义好模板，你可以把它放在textarea文本框里如：  
&lt;textarea id="txtTemplate" style="display:none"&gt;  
&lt;![CDATA[  
hello {$T.name}.  
]]&gt;  
&lt;/textarea&gt;  
这样就定了一个简单的模板，下面再进行模板数据的定义：  
var mydata = { name: 'Anne', age: '20' };  
定义模板显示容器：  
&lt;div id="result1"&gt;&lt;/div&gt;  
通过简单的两行代码就可以完成模板的执行工作：  
$("#result1").setTemplateElement("txtTemplate");  
$("#result1").processTemplate(mydata);  
显示结果：  
hello Anne.

如何？是不是很简单方便且实用！

别急，这只是jTemplates强大功能的冰山一角。

jTemplates包含三个预定全局变量，分别是**$T、$P、$Q**。$T为模板提供数据调用功能，$P为模板提供参数变量调用功能，$Q.version提供当前jTemplate的版本  
下面介绍将会用到这些功能。

jTemplates还支持**#if、#for、#cycle、#foreach、#include、#param**标签，帮助你处理实现复杂的业务情形。  
#if 语法：  
**{#if |COND|}..{#elseif |COND|}..{#else}..{#/if}**  
#if 示例:  
{#if $T.hello} hello world. {#/if}  
{#if 2\*8==16} good {#else} fail {#/if}  
{#if $T.age&gt;=18} 成人了 {#else} 未成年 {#/if}  
{#if $T.list\_id == 3} System list {#elseif $T.list\_id == 4} Users List {#elseif $T.list\_id == 5} Errors list {#/if}

**#for 语法：**  
**{#for |VAR| = |CODE| to |CODE| \[step=|CODE|]}..{#else}..{#/for}**  
或  
**{#for |variable| = |start| to |end| \[step=|stepBy|]}..{#else}..{#/for}**  
#for 示例：  
默认步长：{#for index = 1 to 10} {$T.index} {#/for}   
正向步长：{#for index = 1 to 10 step=3} {$T.index} {#/for}  
负向步长及空循环：{#for index = 1 to 10 step=-3} {$T.index} {#else} nothing {#/for}  
也可以在循环中使用变量：{#for index = $T.start to $T.end step=$T.step} {$T.index} {#/for}  
说明：{#else}是在{#for...}未能执行的时的输出内容。

**#foreach 语法：**  
**{#foreach |VAR| as |NAME| \[begin=|CODE|] \[count=|CODE|] \[step=|CODE|]}..{#else}..{#/for}**  
#foreach 示例：  
默认：{#foreach $T.table as record} {$T.record.name} {#/for}  
指定起始位置：{#foreach $T.table as record begin=1} {$T.record.name} {#/for}  
指定起始和循环次数：{#foreach $T.table as record begin=1 count=2} {$T.record.name} {#/for}  
指定步长：{#foreach $T.table as record step=2} {$T.record.name} {#/for}  
#foreach 内定环境变量：  
$index - index of element in table  
$iteration - id of iteration (next number begin from 0)  
$first - is first iteration?  
$last - is last iteration?  
$total - total number of iterations  
$key - key in object (name of element) (0.6.0+)  
$typeof - type of element (0.6.0+)  
#foreach 示例所需要的数据：  
var data = {  
name: 'User list',  
list\_id: 4,  
table: [  
{id: 1, name: 'Anne', age: 22, mail: 'anne@domain.com'},  
{id: 2, name: 'Amelie', age: 24, mail: 'amelie@domain.com'},  
{id: 3, name: 'Polly', age: 18, mail: 'polly@domain.com'},  
{id: 4, name: 'Alice', age: 26, mail: 'alice@domain.com'},  
{id: 5, name: 'Martha', age: 25, mail: 'martha@domain.com'}  
]  
};  
***(0.7.0+)版以后新增的功能，支持待循环集合用函数代替：***  
{#foreach |FUNC| as |NAME| \[begin=|CODE|] \[end=|CODE|] \[count=|CODE|] \[step=|CODE|]}..{#else}..{#/for}  
例：  
f = function(step) {  
if(step &gt; 100) return null; // stop if loop is too long  
return "Step " + step;  
};

$("#result").setTemplate("{#foreach f as funcValue begin=10 end=20} {$T.funcValue}&lt;br/&gt; {#/for}");  
$("#result").processTemplate();  
#foreach在每次循环时请求的就是f函数，然后传递参数给f使用，并返回结果给funcValue变量

**#cycle 语法：**  
**{#cycle values=|ARRAY|}**  
功能：提供周期性的调用，在循环中实现交替样式功能时可用到  
示例：  
{#cycle values=\[1,2,3,4]}  
下面模板在执行循环时，就会周期性的调用#cycle数组中的值，这样就能实现行交替的效果:  
&lt;table width=\\"200\\"&gt;  
{#foreach $T.table as row}  
&lt;tr bgcolor=\\"{#cycle values=\['#AAAAAA','#CCCCCC']}\\"&gt;  
&lt;td&gt;{$T.row.name.link('mailto:'+$T.row.mail)}&lt;/td&gt;  
&lt;/tr&gt;  
{#/for}  
&lt;/table&gt;

**#include 语法：**  
**{#include |NAME| \[root=|VAR|]}**  
功能：提供子模板调用  
示例：  
{#template MAIN}  
{* this is comment \*}  
{$T} {* $T == $T.toSource() \*}  
&lt;table&gt;  
{#foreach $T.table as record}  
{#include ROW root=$T.record}  
{#/for}  
&lt;/table&gt;  
{#/template MAIN}

{#template ROW}  
&lt;tr class="{#cycle values=\['bcEEC','bcCEE']}"&gt;  
&lt;td&gt;{$T.name}&lt;/td&gt;  
&lt;td&gt;{$T.mail}&lt;/td&gt;  
&lt;/tr&gt;  
{#/template ROW}  
说明：{#template MAIN} 是指定模板的主要部分，必不可少。  
{#template ROW}是定义一个名为“ROW”的子模板。  
{#include ROW root=$T.record}是主模板调用“ROW”子模板，并传递参数$T.record

**#param 语法：**  
{#param name=|NAME| value=|CODE|}  
功能：定义模板内的局部变量参数，使用$P调用。  
示例：  
$("#result").setTemplate("{#param name=x value=888}{$P.x}");  
$("#result").processTemplate();  
输出结果：888  
示例：  
$("#result").setTemplate("{#param name=x value=$P.x+1}{$P.x}");  
$("#result").setParam('x', 777);  
$("#result").processTemplate();  
输出结果：778  
示例：  
$("#result").setTemplate("&lt;ul&gt;{#foreach $T.table as row}&lt;li&gt;{$P.x} {$T.row.name}&lt;/li&gt;{#param name=x value=$P.x+3}{#/for}&lt;ul&gt;");  
$("#result").setParam('x', 1);  
$("#result").processTemplate(data);  
需要数据：  
var data = {  
name: 'User list',  
list\_id: 4,  
table: [  
{id: 1, name: 'Anne', age: 22, mail: 'anne@domain.com'},  
{id: 2, name: 'Amelie', age: 24, mail: 'amelie@domain.com'},  
{id: 3, name: 'Polly', age: 18, mail: 'polly@domain.com'},  
{id: 4, name: 'Alice', age: 26, mail: 'alice@domain.com'},  
{id: 5, name: 'Martha', age: 25, mail: 'martha@domain.com'}  
]  
};  
输出结果：  
\# 1 Anne  
\# 4 Amelia  
\# 7 Polly  
\# 10 Alice  
\# 13 Martha

转自 [http://hi.baidu.com/dongyuejiang/item/4e4ea8c60ac4f362f7c95dab](http://hi.baidu.com/dongyuejiang/item/4e4ea8c60ac4f362f7c95dab)