热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

jTemplates部分语法介绍

1.{#if}{#if|COND|}..{#elseif|COND|}..{#else}..{#if}Examples:{#if2*816}good{#else}fa
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}
{#/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}
{#/for}

 

Anne
Amelie

 



New (0.6.0+):
#foreach on object:

$('#result').setTemplate('{#foreach $T as i}{$T.i$key} - {$T.i}
{#/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}
{#/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}
{#/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('{$T.name} ({$T.age})');
$('#result').setTemplate('{#include t1 root=$T.table[0]}', {t1: template1});
$('#result').processTemplate(data);



Result:

Anne (22)

 

var template1 = $.createTemplate('

{$T.name} ({$T.age})
');
$('#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}
 


 
{$T.name.bold()}

  {#include table root=$T.table}
 

{#/template MAIN}

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

*** main table ***
{#template table}
 
  {#foreach $T as r}
  {#include row root=$T.r}
  {#/for}
 

{#/template table}

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

*** for each row ***
{#template row}
  bgcolor="{#cycle values=['#AAAAEE','#CCCCFF']}">
  {$T.name.bold()}
  {$T.age}
  {$T.mail.link('mailto:'+$T.mail)}
 
{#/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

 


推荐阅读
  • vue使用
    关键词: ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 成功安装Sabayon Linux在thinkpad X60上的经验分享
    本文分享了作者在国庆期间在thinkpad X60上成功安装Sabayon Linux的经验。通过修改CHOST和执行emerge命令,作者顺利完成了安装过程。Sabayon Linux是一个基于Gentoo Linux的发行版,可以将电脑快速转变为一个功能强大的系统。除了作为一个live DVD使用外,Sabayon Linux还可以被安装在硬盘上,方便用户使用。 ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • C++中的三角函数计算及其应用
    本文介绍了C++中的三角函数的计算方法和应用,包括计算余弦、正弦、正切值以及反三角函数求对应的弧度制角度的示例代码。代码中使用了C++的数学库和命名空间,通过赋值和输出语句实现了三角函数的计算和结果显示。通过学习本文,读者可以了解到C++中三角函数的基本用法和应用场景。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • 本文介绍了使用哈夫曼树实现文件压缩和解压的方法。首先对数据结构课程设计中的代码进行了分析,包括使用时间调用、常量定义和统计文件中各个字符时相关的结构体。然后讨论了哈夫曼树的实现原理和算法。最后介绍了文件压缩和解压的具体步骤,包括字符统计、构建哈夫曼树、生成编码表、编码和解码过程。通过实例演示了文件压缩和解压的效果。本文的内容对于理解哈夫曼树的实现原理和应用具有一定的参考价值。 ... [详细]
author-avatar
手机用户2502933677
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有