1.{#if}

{#if |COND|}..{#elseif |COND|}..{#else}..{#/if}

Examples:

{#if 2*8==16} good {#else} fail {#/if}

Return:

good

{#if $T.list_id == 5} good {#else} fail {#/if}

Return:

fail

(check 'data' below)

{#if $T.list_id} {$T.list_id} {#/if}

Return:

4

{#if $T.list_id == 3} System list {#elseif $T.list_id == 4} Users List {#elseif $T.list_id == 5} Errors list {#/if}

Return:

Users List

Code:

Data: (click to hide/show)

Results:

good

2.{#foreach}

Go for each element in table:

{#foreach |VAR| as |NAME| [begin=|CODE|] [count=|CODE|] [step=|CODE|]}..{#else}..{#/for}

{#else} and begin, count, step are optional.



Examples:



Write all names:

{#foreach $T.table as record} {$T.record.name} {#/for}

Result:

Anne Amelia Polly Alice Martha

Write names begin with second:

{#foreach $T.table as record begin=1} {$T.record.name} {#/for}

Result:

Amelia Polly Alice Martha

Write only two names begin with second:

{#foreach $T.table as record begin=1 count=2} {$T.record.name} {#/for}

Result:

Amelia Polly

Using step:

{#foreach $T.table as record step=2} {$T.record.name} {#/for}

Result:

Anne Polly Martha

Example with {#else}.

{#foreach $T.table as record begin=8} {$T.record.name} {#else} none {#/for}

Result:

none

Foreach set variables:

$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+)



Example:

Template is long, so I copy it to file: example_foreach1.tpl

Usage:

$("#result").setTemplateURL('example_foreach1.tpl');

$("#result").processTemplate(data);

It should give result:

Index Iterator Name Age First? Last?

1 0 Amelia 24 true false

2 1 Polly 18 false false

3 2 Alice 26 false false

4 3 Martha 25 false true


New (0.7.8+):

Break and Continue:



Example 1: Skip item with id == 3

{#foreach $T.table as i}{#if $T.i.id==3}{#continue}{#/if}{$T.i.name}<br/>{#/for}

Anne

Amelie

Alice

Martha

Example 2: Break at element with id == 3

{#foreach $T.table as i}{#if $T.i.id==3}{#break}{#/if}{$T.i.name}<br/>{#/for}

Anne

Amelie


New (0.6.0+):

#foreach on object:

$('#result').setTemplate('{#foreach $T as i}{$T.i$key} - {$T.i}<br/>{#/for}');

$('#result').processTemplate({'a':1, 'b':2, 'c':3});

Variable $key display key for current element in object.


New (0.7.0+):

#foreach with function:

{#foreach |FUNC| as |NAME| [begin=|CODE|] [end=|CODE|] [count=|CODE|] [step=|CODE|]}..{#else}..{#/for}

Loop work till out of range (set by begin, end, count) or function |FUNC| return undefined/null.



Examples:

f = function(step) {

  if(step
> 100)
return null;
 // stop if loop is too long

  return "Step " + step;

};



$("#result").setTemplate("{#foreach f as
funcValue begin=10 end=20} {$T.funcValue}<br/> {#/for}");

$("#result").processTemplate();

Step 10

Step 11

Step 12

Step 13

Step 14

Step 15

Step 16

Step 17

Step 18

Step 19

Step 20

Try remove end limit, loop will be break on index 100.



Please use this features with carefull!

Code:

Data: (click to hide/show)

Results:

Anne Amelie Polly Alice Martha

3.{#for}

Syntax:

{#for |VAR| = |CODE| to |CODE| [step=|CODE|]}..{#else}..{#/for}

Using as:

{#for |variable| = |start| to |end| [step=|stepBy|]}..{#else}..{#/for}

|variable| - name of variable, ex: i, index (please not use
javascript's reserved words).

|start| - loop begin value, ex: 1, $T.start

|end| - loop end value, ex: 10, $T.end

|stepBy| - step, ex: 1, -1 (downto)



Examples:

{#for index = 1 to 10} {$T.index} {#/for}

Result:

1 2 3 4 5 6 7 8 9 10

{#for index = 1 to 10 step=3} {$T.index} {#/for}

Result:

1 4 7 10

{#for index = 1 to 10 step=-3} {$T.index} {#else} nothing {#/for}

Result:

nothing

{#for index = 10 to 0 step=-3} {$T.index} {#/for}

Result:

10 7 4 1

$("#result").setTemplate("{#for index = $T.start to $T.end}
{$T.content}{$T.index}<br/> {#/for}");

$("#result").processTemplate({start: 2,
end: 5, content: "ID:
"});

ID: 2

ID: 3

ID: 4

ID: 5

Code:

Data: (click to hide/show)

Results:

Message: 1

Message: 2

Message: 3

Message: 4

Message: 5

Message: 6

Message: 7

Message: 8

API

4.
{#include}

Include other template.

{#include |NAME| [root=|VAR|]}

Examples:

var template1 = $.createTemplate('<b>{$T.name}</b>
({$T.age})');

$('#result').setTemplate('{#include t1 root=$T.table[0]}', {t1: template1});

$('#result').processTemplate(data);

Result:

Anne (22)

var template1 = $.createTemplate('<div><b>{$T.name}</b>
({$T.age})</div>');

$('#result').setTemplate('{#foreach $T.table as row}{#include t1 root=$T.row}{#/for}', {t1: template1});

$('#result').processTemplate(data);

Result:

Anne (22)

Amelia (24)

Polly (18)

Alice (26)

Martha (25)

Code:

Data: (click to hide/show)

Results:

Anne (22)

API

5.
multiple
templates

Since jTemplates 0.2 allowed is using multiple templates
in one file.



Example:

*** main template *** (all part outside templates are invisible}

{#template MAIN}

 <div>

  <div>{$T.name.bold()}</div>

  {#include table root=$T.table}

 </div>

{#/template MAIN}



-----------------------------------------



*** main table ***

{#template table}

 <table>

  {#foreach $T as r}

  {#include row root=$T.r}

  {#/for}

 </table>

{#/template table}



-----------------------------------------



*** for each row ***

{#template row}

 <tr bgcolor="{#cycle values=['#AAAAEE','#CCCCFF']}">

  <td>{$T.name.bold()}</td>

  <td>{$T.age}</td>

  <td>{$T.mail.link('mailto:'+$T.mail)}</td>

 </tr>

{#/template row}

Above template in file: example_multitemplate1.tpl


Subtemplates can use different setting than main one. Current implementation
does not allow to change most settings, like filter_data, etc, thus this
feature are not really useful. Example:

{#template item runnable_functions=false}

...

{#/template list}

Code:

Data: (click to hide/show)

Results:

User list

Anne

22

anne@domain.com

Amelie

24

amelie@domain.com

Polly

18

polly@domain.com

Alice

26

alice@domain.com

Martha

25

martha@domain.com

API

jTemplates部分语法介绍的更多相关文章

  1. Swift翻译之-Swift语法入门 Swift语法介绍

    目录[-] Hello world - Swift 简单赋值 控制流 函数与闭包 对象和类 枚举与结构 协议和扩展 泛型 2014.6.3日,苹果公布最新编程语言Swift,Swift是一种新的编程语 ...

  2. flex弹性布局语法介绍及使用

    一.语法介绍 Flex布局(弹性布局) ,一种新的布局解决方案 可简单.快速的实现网页布局 目前市面浏览器已全部支持1.指定容器为flex布局 display: flex; Webkit内核的浏览器, ...

  3. freemarker语法介绍及其入门教程实例

    # freemarker语法介绍及其入门教程实例 # ## FreeMarker标签使用 #####一.FreeMarker模板文件主要有4个部分组成</br>####  1.文本,直接输 ...

  4. QSS类的用法及基本语法介绍

    QSS类的用法及基本语法介绍 目录 1. 何为Qt样式表2. 样式表语法基础3. 方箱模型4. 前景与背景5. 创建可缩放样式6. 控制大小7. 处理伪状态8. 使用子部件定义微观样式8.1. 相对定 ...

  5. MD基本语法介绍

    Markdown基本语法介绍 前言 文本编辑器一般用的有富文本编辑器(也就是Word)和md了,但是wold太过于花里胡哨很多功能都用不上,所以就选择md了,简单实用,一对于我来说一般就用标题和列表就 ...

  6. Markdown 语法介绍

    Markdown 语法介绍 from:https://coding.net/help/doc/project/markdown.html 文章内容 1 Markdown 语法介绍 1.1 标题 1.2 ...

  7. css基本概念与css核心语法介绍

    css基本概念 css是什么?不需要了解太多文字类介绍,记住css是层叠样式表,HTML是页面结构,css负责页面样式,javascrt负责静态页面的交互.CSS 能够对网页中元素位置的排版进行像素级 ...

  8. c基本语法介绍

    c语言基本语法介绍 1.把常量定义为大写字母形式,是一个很好的编程实践.

  9. [安卓基础]011存储数据(中)——sqlite语法介绍

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

随机推荐

  1. 【Knockout.js 学习体验之旅】(2)花式捆绑

    本文是[Knockout.js 学习体验之旅]系列文章的第2篇,所有demo均基于目前knockout.js的最新版本(3.4.0).小茄才识有限,文中若有不当之处,还望大家指出. 目录: [Knoc ...

  2. .NET基础拾遗(2)面向对象的实现和异常的处理基础

    Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理基础 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开 ...

  3. Unity3D和Egret3D的基情

    Unity3D依靠多平台发布这个核心特点,目前如日中天,屌丝引擎之王绝无来者.Egret白鹭引擎,也着实在微信上刷了一屏又一屏.这二者似乎风马牛不相及,但是这个无处不搞基的年代,让一切皆有可能. U3 ...

  4. [MongoDB] 32Bit构建上文件大小限制问题

    一. 问题概述 今天看看爬虫抓取的数据,发现数据无法插入,首先想到的就是32Bit构建的文件大小限制问题,检查一下还真的是.本文把整个检查问题,解决问题的过程记录下来. 问题:     "s ...

  5. ABP(现代ASP.NET样板开发框架)系列之9、ABP设置管理

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之9.ABP设置管理 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)” ...

  6. ELK 安装笔记

    logstash -noverify -javaagent:E:\svn\other\nn\jrebel6.0.0+crack\jrebel6.0.0-crack\jrebel.jar -Drebel ...

  7. 《ES6基础教程》之 map、forEach、filter indexOf 用法

    1,map,对数组的每个元素进行一定操作,返回一个新的数组. var oldArr = [{first_name:"Colin",last_name:"Toh" ...

  8. Handler系列之原理分析

    上一节我们讲解了Handler的基本使用方法,也是平时大家用到的最多的使用方式.那么本节让我们来学习一下Handler的工作原理吧!!! 我们知道Android中我们只能在ui线程(主线程)更新ui信 ...

  9. window.name实现的跨域数据传输

    这篇文章是对 JavaScript跨域总结与解决办法 的补充. 有三个页面: a.com/app.html:应用页面. a.com/proxy.html:代理文件,一般是一个没有任何内容的html文件 ...

  10. jQuery2.x源码解析(设计篇)

    jQuery2.x源码解析(构建篇) jQuery2.x源码解析(设计篇) jQuery2.x源码解析(回调篇) jQuery2.x源码解析(缓存篇) 这一篇笔者主要以设计的角度探索jQuery的源代 ...