热门标签 | 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

 


推荐阅读
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 使用nodejs爬取b站番剧数据,计算最佳追番推荐
    本文介绍了如何使用nodejs爬取b站番剧数据,并通过计算得出最佳追番推荐。通过调用相关接口获取番剧数据和评分数据,以及使用相应的算法进行计算。该方法可以帮助用户找到适合自己的番剧进行观看。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 本文介绍了C++中省略号类型和参数个数不确定函数参数的使用方法,并提供了一个范例。通过宏定义的方式,可以方便地处理不定参数的情况。文章中给出了具体的代码实现,并对代码进行了解释和说明。这对于需要处理不定参数的情况的程序员来说,是一个很有用的参考资料。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 本文介绍了为什么要使用多进程处理TCP服务端,多进程的好处包括可靠性高和处理大量数据时速度快。然而,多进程不能共享进程空间,因此有一些变量不能共享。文章还提供了使用多进程实现TCP服务端的代码,并对代码进行了详细注释。 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
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社区 版权所有