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

 


推荐阅读
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 本文基于刘洪波老师的《英文词根词缀精讲》,深入探讨了多个重要词根词缀的起源及其相关词汇,帮助读者更好地理解和记忆英语单词。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 360SRC安全应急响应:从漏洞提交到修复的全过程
    本文详细介绍了360SRC平台处理一起关键安全事件的过程,涵盖从漏洞提交、验证、排查到最终修复的各个环节。通过这一案例,展示了360在安全应急响应方面的专业能力和严谨态度。 ... [详细]
  • 尽管使用TensorFlow和PyTorch等成熟框架可以显著降低实现递归神经网络(RNN)的门槛,但对于初学者来说,理解其底层原理至关重要。本文将引导您使用NumPy从头构建一个用于自然语言处理(NLP)的RNN模型。 ... [详细]
  • 基因组浏览器中的Wig格式解析
    本文详细介绍了Wiggle(Wig)格式及其在基因组浏览器中的应用,涵盖variableStep和fixedStep两种主要格式的特点、适用场景及具体使用方法。同时,还提供了关于数据值和自定义参数的补充信息。 ... [详细]
  • MySQL索引详解与优化
    本文深入探讨了MySQL中的索引机制,包括索引的基本概念、优势与劣势、分类及其实现原理,并详细介绍了索引的使用场景和优化技巧。通过具体示例,帮助读者更好地理解和应用索引以提升数据库性能。 ... [详细]
  • 本文探讨了 Spring Boot 应用程序在不同配置下支持的最大并发连接数,重点分析了内置服务器(如 Tomcat、Jetty 和 Undertow)的默认设置及其对性能的影响。 ... [详细]
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社区 版权所有