`

《Velocity 模板使用指南》中文版

阅读更多

1.本文目地和使用对象

2.什么是Velocity?

3.Velocity能为你做什么?

1.一个Mud Store Example

4.Velocity Template Language (VTL): 介绍

5.输出第一个VTL页面!

6.Comments(注释)

7.References(引用)

1.Variables(变量)

2.Properties(属性)

3.Methods(命令)

4.属性引用中的属性查找规则

8.Formal Reference Notation(正规引用格式注意事项)

9.Quiet Reference Notation(静态引用输出)

10.Getting literal( 语义问题)

1.Currency(货币标志)

2.Escaping Valid VTL References(封装有效的引用)

11.Case Substitution(可选的格式)

12.Directives(指令符号)

1.#set指令

2.Literals (语义解析)

3.Conditionals(条件判断)

4.Loops(循环)

3.Include(引入)

6.Parse(解析模板)

7.Stop

8.Velocimacros(宏调用)

13.注掉 VTL Directives

14.VTL: 一般使用的格式

15.Other Features and Miscellany(其它特性和细节)

1.数学计算

2.Range Operator

3.Advanced Issues: Escaping and !

4.Velocimacro Miscellany(关于宏的一些问题)

5.String Concatenation(连结字符串)

16.Feedback

1.本文目地和使用对象

本文主要介绍如何在模板中使用Velocity功能强大的模板语言VTL(Velocity Template Language)用法有一个全面的认识,并掌握如何在模板中有效使用。同时,本文提供较多的例子帮您来学习它.

感谢您选择Velocity帮助您实现纯正的MVC系统构架。

2.什么是Velocity?

Velocity 是一个基于java的模板引擎(template engine). 它可以让视图的设计者在web页面中引用java代码中定义的数据对象和命令。从而使Web designers和java开发者依照MVC思想(Model-View-Controller )开发系统,这意味着Web designers只须将精力注用于良好表现力的视图外观设计,而Java程序员则只要关心着如何写出高效简洁的java对象以实现业务逻辑 -----Velocity会将他们组装到一起. 相比传统的jsp、PHP等脚本语言,Velocity彻底的将避免了在视图设计中出现的java代码, 从而保证了web site的长期可维护性.

一 定要理解,Velocity是一个template engine的意思,它还可以从模板中生成SQL语句或其它脚本提供给web pages. 它也可以独立使用---做为一个工具类(utility class)用来生成源代码、报表、邮件模板等---在有需要重复的视图情况下,你应想到使用Velocity.Apache站点提供的另外一个框架Turbine 可以和Velocity有效结合以实现true MVC model.

3.Velocity能为你做什么?

1.一个Mud Store Example

假 设你是一个page designer 在为一个在线商店设计页面. 我们称这个项目为 "The Online Mud Store".业务发展还不错,客户会订购不同类型的MuD,每个客户都会用自己的帐号密码login,查看选择他们订购的MuD,查看订单,但还有些忠 诚用户会购卖不太流行的MuD----这些不需要出现在页面中显眼的地方。当然,The Online MuD Store必须把每个客户资料及订购信息记录到DB中,现在的问题是,如何让某个客户login后就看到他感兴趣的信息?

使用Velocity!我们为每个客户定制一个页面!这听起来工作量巨大,让我们试试:.

使用Velocity的 VTL 如下来设计 web page:

<HTML>

<BODY>

##指定用户名字

欢迎你: $customer.Name!

<table>

###输出用户喜好的MuD

#foreach( $mud in $mudsOnSpecial )

#if ( $customer.hasPurchased($mud) )

<tr>

<td>

$flogger.getPromo( $mud )

</td>

</tr>

#end

#end

</table>

使用VTL设计页面就是这么简单!文档《VTL参考中文版》中有更全面的VTL语言介绍,掌握这些,你将会全面体会到Velocity的威力.

4.Velocity Template Language (VTL): 介绍

The Velocity Template Language (VTL)目标是提供一个简洁,易学的方法将动态内容展现到web page上. a web page设计者可以没有任何编程经验就可以在一天内学会使用它增强你的站点的展示力!.

VTL使用引用(references )这种方式将dynamic content(动态内容,一般指java代码生成的数据对象)加入到你的web site,Velocity中的变量(variable)只是refernce中的一种. Variables是用来描述从引入到视图模板中的java数据对象。当然,java代码也可以从模板的VTL中获取数据.以下是一个写在HTML中的 VTL变量:

#set( $a = "Velocity" )

VTL声明( statement),所以的VTL statement都是以#开头,且包含一个指示符(这里是set),当客户访问你的页面时, the Velocity Templating Engine将搜索页面中的所有 # 符号,如果确定这是一个VTL声明时就按一定规则处理动态内容, 符号#仅仅只是表明这可能是一个VTL声明.

符号# 所跟的set我们用“指示符”这一名词来称呼它(随后介绍更多的指示符), set 指示符使用一个表达式(expression) (包含在一对括号里) –将一个值 value (这里是Velocity)付给变量a,(变量名在左边,值在右边,用=组合起来).

在以上的例子中,变量是a ,而符号“$”表明它是一个变量,Velocity中所有变量以符号”$”开头,所付的值要用双引号括起, 这个值中还可以再添加Velocity变量,如"Hello $name",输出的将是name变量所付的值。

这是理解VTL基础的规则:

以$开头的表示“引用”意思是取得一些东东.而”指示”(Directives)则以#开头来表示,有点“做些什么动作”的意思.

如上便, #set 用来指定值给一个变量名$a, 以“$”标示的变量名a的值就是"Velocity".

5.输出第一个VTL页面!

有你的HTML文档的任何地方,都可以引用一个变量名来输出值, 如下例, 先给变量名foo 赋值为Velocity,然后将它输出到页面中.

<html>

<body>

#set( $foo = "Velocity" )

Hello $foo World!

</body>

<html>

在这个页面上,你看到的将是 "Hello Velocity World!".

为了让查板中的VTL指令更易读, 我们强烈建议你每行一条VTL指令,当然这不是必须的. 关于 set directive的更多功能我们随后再讨论.

6.Comments(注释)

Comments可以让你在模板中包含对VTL或其它问题的说明描述以便与阅读和理解.---但它并不会在最终输出的web pages中看到.如下示例是VTL中的一行注释.

## This is a single line comment.

单行注释是以## 开头的一行文字.如要写下多行注释,就要像下面那样,将它们放入#**#间:

This is text that is outside the multi-line comment.

Online visitors can see it.

#*

Thus begins a multi-line comment. Online visitors won't

see this text because the Velocity Templating Engine will

ignore it.

*#

不需要太复杂了,这两种方式己足够你给自己的页面加上充分的说明。

7.References(引用)

VTL 中有三种references:变量引用(variables),属性引用(properties)和命令引用(methods). 做为一个使用VTL的设计者, 你和你的java软件工程师必须就模板中引用的特定名了(就是$后的名字)达成一致的协议!这样,模板和java代码才可按照你们的意图去结合以输出正确 的内容.

所有的引用在模板中都表现为一个字符串. 假设一个引用变量 $foo 的值事实上是一个int, Velocity engine在处理时将调用它的.toString() 去解析这个字符串所代表的对象(int).

1.Variables(变量)

简单的说变量以"$" 开后,后面跟一个VTL指示符( Identifier).一个合法的VTL指示符是以字母开头,后面可以是以下任意字符:

alphabetic (a .. z, A .. Z)

numeric (0 .. 9)

hyphen ("-")

underscore ("_")

以下是正确的VTL变量名:

$foo

$mudSlinger

$mud-slinger

$mud_slinger

$mudSlinger1

当VTL中的一个变量如 $foo, 这个变量可以在模板中提取它自己的值通过set 指示符,或者从java代码中.比如,如果需java中的变量foo的值为bar,给模板中所有输出$foo声明的值都成为bar.当然,在模板中使用如下VTL也可以达到这个目地。

#set( $foo = "bar" )

2.Properties(属性)

VTL的第二种引用,属性引用,注意”属性”具有相对固定的格式. 它也是以$开头的合法VTL指示符,随后是”变量名字.变量属性”. 如下例:

$customer.Address

$purchase.Total

第一个例子, $customer.Address.我们设想可能在两种意思.首先它可能在查找customer引用的一个hashtable中的以“Address”为key关联的一个数据对象.另外他可能表示的是java对象customer中的getAddress()这个命令取得的结果(当然也可写成 $customer.getAddress()). 当客户请求Web页面中Velocity将根据具体的customer类型输出.

3.Methods(命令)

在java 代码中定义命令是最通常的事, 像执得一组方程式计算成本,读取一个文件等. 命令引用和其它引用一个也是一个一般的VTL声明,看如下的例子可能会更快的理解:

$customer.getAddress()

$purchase.getTotal()

$page.setTitle( "My Home Page" )

$person.setAttributes( ["Strange", "Weird", "Excited"] )

前两个例子 -- $customer.getAddress() and $purchase.getTotal() – 可以等同与属性引用情况: $customer.Address and $purchase.Total. 如你猜对了这点,呵,你还蛮聪明!同时,他们是等同与java对象coustomer的getAddress()命令调用。后面两个引用呢,也会直接对应java对象的对应命令,不同的是传入了命令参数。

4.属性引用中的属性查找规则

前己提及,属性可以引用到对象的命令. Velocity会使用合适的策略选择引用到的命令. 它会根据协定的命令命令格式查找. 无论属性引用的的名字是否大小写,Velocity都有固定的查找规则.如在$customer.address引用时,查找顺序是:

  1. getaddress()

  2. getAddress()

  3. get("address")

  4. isAddress()

对于VTL中大写的属性名Address引用,将是:

  1. getAddress()

  2. getaddress()

  3. get("Address")

  4. isAddress()

8.Formal Reference Notation(正规引用格式注意事项)

以上是简洁格式引用的介绍,规则的格式像下面,如你看到的变量名需要放到{}中:

${mudSlinger}

${customer.Address}

${purchase.getTotal()}

大多数情况下,简洁格式引用足以满足使用,但有些情况下,必须使用正规格式引用.

假设你构造的一个字符串中要包括有 $vice 变量的值. 你觉得以下两种写法会是同样的结果吗:

1.Jack is a $vicemaniac.

2.Jack is a ${vice}maniac.

这样,Velocity就知你要的是 $vice, 而不是 $vicemaniac变量,正规引用格式一般用于在模板中直接调整字符串内容.

9.Quiet Reference Notation(静态引用输出)

Velocity遇到一个不能处理的引用时,一般他会直接输出这个引用$email的写法,页面上会看到的是$email,如下例,我们可以在$后面加上一个!号,那么就会输出空白:.

<input type="text" name="email" value="$email"/>

<input type="text" name="email" value="$!email"/>

正式的写法是:.

<input type="text" name="email" value="$!{email}"/>

10.Getting literal( 语义问题)

一般情况下,velocity使用$,#字符来标志它的声明,但有时,HTML中因为某种其它意图,也会写出这样的字符,我们讨论如何消除这种语义歧义问题.

1.Currency(货币标志)

如美元 $2.50!这样的写法出现到模板中, VTL处理时是不会出错,会正确的输出$2.50!这个你想要的结果。为什么呢?一个合法的VTL标示符是以一个字母开头的,我们前面己说过.

2.Escaping Valid VTL References(封装有效的引用)

如下示,如果没有#set( $email = "foo" )这一行且java代码中Context对象中没有放放email对象,将直接输出$email.

#set( $email = "foo" )

$email

如果email己定义了 (比如它的值是 foo),而这里你却想输出 $email. 这样一个字符串,就需要使用转义字符”\”.

## The following line defines $email in this template:

#set( $email = "foo" )

$email

\$email

\\$email

\\\$email

上面的模板在web页面上的输出将是:

foo

$email

\foo

\$email

但如果email并没有定义,我们这样写:.

$email

\$email

\\$email

\\\$email

输出就原封不动了:

$email

\$email

\\$email

\\\$email

注意:当己定义变量和未定义变量一起输出时,会输出字面意思,如下便,$moon是未定义的:

#set( $foo = "gibbous" )

$moon = $foo

输出到web页面中将是

$moon = gibbou

11.Case Substitution(可选的格式)

至此,你对velocity的refenerce己比较熟悉了, 你可以在你的模板中开始应用这些功能. 但你还可以知道的是Velocity references从java语法中汲取了一些优点以便模板设计者更容易使用VTL.比如:

$foo

$foo.getBar()

## 等同于

$foo.Bar

$data.setUser("jon")

##等同于

#set( $data.User = "jon" )

$data.getRequest().getServerName()

##等同于

$data.Request.ServerName

## is the same as

${data.Request.ServerName}

这里示例了你可选的一些引用方式. VTL汲取了java语法和java bean的一些简洁语法以解析java代码中Context中的对象和这些对象的命令及属性---这样,一个java对象的所有功能都可以展示到视图中了.

Velocity也借见了java Bean的规范(Bean specifications defined by Sun Microsystems), 是大小写敏感的; 但Velocity会尽可能的帮你修正错误. 当命令 getFoo() 通过指令 $bar.foo在模板中引用时,Velocity的搜索规则我们在前面己讲了,你还记得是什么吗?.

注意:模板中引用的必须是通过java Bean中的getter/setter 实现的,而直接的java对象的数据域是不能直接引用的,如$foo.Name 会解析到 class Foo's getName() 的实例方法,但不会解析到Foos类的 public Name 这个实例变量.

12.Directives(指令符号)

模板设计者使用“引用“生成动态内容, 指令(directives) – 简单的说就是设计者在模板中操作java对象—让视图设计者全面控制输出内容的格式.

指令总是以 #开头后面紧跟具体的指令符. 就像引用一样(指令的一种),可以将指令理解为”表示这里是一个什么东东).如下例生成一个出错提示:

#if($a==1)true enough#elseno way!#end

这个例子中应使用括号将else分开.

#if($a==1)true enough#{else}no way!#end

1.#set指令

#set 用来给一个引用赋值.值可以被赋给变量引用或属性引用, 但要将它们放入括号中,如下示:

#set( $primate = "monkey" )

#set( $customer.Behavior = $primate )

“左操作数被赋值“是引用操作的一个规则.=号右侧可能是以下类型之一:

  • Variable reference变量引用

  • String literal字符串

  • Property reference 属性引用

  • Method reference 命令引用

  • Number literal 数字

  • ArrayList 数组

  • Map 映射

下面是对上述类型设置的示例:

#set( $monkey = $bill ) ## variable reference

#set( $monkey.Friend = "monica" ) ## string literal

#set( $monkey.Blame = $whitehouse.Leak ) ## property reference

#set( $monkey.Plan = $spindoctor.weave($web) ) ## method reference

#set( $monkey.Number = 123 ) ##number literal

#set( $monkey.Say = ["Not", $my, "fault"] ) ## ArrayList

#set( $monkey.Map = {"banana" : "good", "roast beef" : "bad"}) ## Map

注意: 在ArrayList类型引用的例子中,其原素定义在数组 [..]中, 因此,你可以使表 $monkey.Say.get(0)访问第一个元素.

类似的,引用Map 的例子中, 原素定义在 { } 中,其键和值间以:隔成一对,使用 $monkey.Map.get("bannana") 在上例中将返回 'good', ( $monkey.Map.banana也会有同样效果).

下面是一般的计算表达式:

#set( $value = $foo + 1 )

#set( $value = $bar - 1 )

#set( $value = $foo * $bar )

#set( $value = $foo / $bar )

但注意:如果右边的操作数是一个属性或命令的引用而返回null,那么赋值将不会成功,且在随后的VTL中也不能再取出使用. 如下例:

#set( $result = $query.criteria("name") )

The result of the first query is $result

#set( $result = $query.criteria("address") )

The result of the second query is $result

如果 $query.criteria("name") 返回的是字符串 "bill", 但 $query.criteria("address") 返回null, 上面的TVL输出结果将是:

The result of the first query is bill

The result of the second query is bill

这对与初学者的理解有些麻烦,比如在 #foreach loops中, 你使用 #set 给一个属性或命令赋值时,如下例示:

#set( $criteria = ["name", "address"] )

#foreach( $criterion in $criteria )

#set( $result = $query.criteria($criterion) )

#if( $result )

Query was successful

#end

#end

在上例中,就不能依赖if( $result )来决定查询是否成功. $result 一但被 #set 为null (context会同样), 它将不能被赋 其它值 (不能从 context中取出).

一个解决办法是,每次都将$result 设为 false. 如果 $query.criteria() 调用成功,就可以检测到.

#set( $criteria = ["name", "address"] )

#foreach( $criterion in $criteria )

#set( $result = false )

#set( $result = $query.criteria($criterion) )

#if( $result )

Query was successful

#end

#end

注意: #set 不需要使用 #end 来声明结尾.

2.Literals (语义解析)

使用#set 指令时,变量如果用 “”引起会被解析,如:

#set( $directoryRoot = "www" )

#set( $templateName = "index.vm" )

#set( $template = "$directoryRoot/$templateName" )

$template

输出的将是:

www/index.vm

但当用单引号引起来时,就不会被解析::

#set( $foo = "bar" )

$foo

#set( $blargh = '$foo' )

$blargh

输出后会是:

bar

$foo

默认情况下,不会解析单引号中的变量,当然,这可以通过改变Velocity的配置参数来改变:

velocity.properties such that stringliterals.interpolate=false.

另外, 指令 #literal 元素可以用来输出字面意思,如下示.

#literal()

#foreach ($woogie in $boogie)

nothing will happen to $woogie

#end

#end

会输出::

#foreach ($woogie in $boogie)

nothing will happen to $woogie

#end

3.Conditionals(条件判断)

1.If / ElseIf / Else

#if 指令用来根据条件在页面中输出内容, 如下简单的例子:

#if( $foo )

<strong>Velocity!</strong>

#end

根据变量 $foo计算后是否为true决定输出, 这会有两种情况: (i) $foo 的是值是一个boolean (true/false)型并有一个true value, 或 (ii) 它是一个非null值. 要记者,Velocity context 中只能包含Objects, 因此当我们讲 'boolean'时, 它就是一个Boolean (the class).

#if#end 的内容是否会输出,由$foo是否为true决定. 这里,如果 $foo is true, 输出将是: "Velocity!". 如果$foo 为null或false,将不会有任何输出.

#elseif#else 可以 #if 和组合使用. 如果第一个表达式为true,将会不计算以后的流程,如下例假设t $foo 是15 and $bar 产 6.

#if( $foo < 10 )

<strong>Go North</strong>

#elseif( $foo == 10 )

<strong>Go East</strong>

#elseif( $bar == 6 )

<strong>Go South</strong>

#else

<strong>Go West</strong>

#end

输出将会是

Go South.

2.Relational and Logical Operators(关系和逻辑运算)

Velocity使用==来做比较,如下例.

#set ($foo = "deoxyribonucleic acid")

#set ($bar = "ribonucleic acid")

#if ($foo == $bar)

In this case it's clear they aren't equivalent. So...

#else

They are not equivalent and this will be the output.

#end

注意:== 计算与java中的 == 计算有些不同:不能用来测试对象是否相等(指向同一块内存). Velocity中是否相等仅直接的用来比较numbers, strings的值, or objects的toString()结果是否相等. 如果是不同的对象,会调用它们的toString()命令结果来比较.

Velocity也使用AND, OR and NOT 执行逻辑运算.详细说明请参看《VTL参考中文版》,如下是一些简单示例:

## logical AND

#if( $foo && $bar )

<strong> This AND that</strong>

#end

仅当 $foo $bar 和都为true时,#if()才会输出中间内容.

OR 运算例子

## logical OR

#if( $foo || $bar )

<strong>This OR That</strong>

#end

$foo或$bar只要有一个为true就可以输出。

NOT运算则只有一个操作参数或表达式 :

##logical NOT

#if( !$foo )

<strong>NOT that</strong>

#end

考虑下,下面的例子有几种输出结果?.

#if( $foo == $bar)it's true!#{else}it's not!#end</li>

4.Loops(循环)

Foreach Loop

#foreach 用来创建循环. For example:

<ul>

#foreach( $product in $allProducts )

<li>$product</li>

#end

</ul>

#foreach 会生成包含$products中对象的一个列表. 每一次循环都会将列表中的一个对象赋与变量$product .

$allProducts 或以是一个Vector, a Hashtable or an Array类型的容器. 指定给变量 $product 是一个引用到其中一个java对象的引用. For example, if $product 确实是一个java代码中的Product class i,它可以这样的方法访问$product.Name method (ie: $Product.getName()).

我们假设 $allProducts 是一个Hashtable.看看取出其中的东东多么简单:

<ul>

#foreach( $key in $allProducts.keySet() )

<li>Key: $key -> Value: $allProducts.get($key)</li>

#end

</ul>

通过引用变量$velocityCount可以访问到Velocity提供的计数器:

<table>

#foreach( $customer in $customerList )

<tr><td>$velocityCount</td><td>$customer.Name</td></tr>

#end

</table>

$velocityCount默认的计数器引用,你可以在配置velocity.properties中改成你喜欢的:

# Default name of the loop counter

# variable reference.

directive.foreach.counter.name = velocityCount

# Default starting value of the loop

# counter variable reference.

directive.foreach.counter.initial.value = 1

当然,你还可以设置其它参数,具体见《Velocity Java开发指南中文版》中讲解.

# The maximum allowed number of loops.

directive.foreach.maxloops = -1

3.Include(引入)

#include 脚本元素让模板设计者可以在模板中引入一个本地文件, 这个被引入的文件将不会经过Velocity的解析. 安全起见,可以引放的文件只是是配置参数TEMPLATE_ROOT所定义目录下的,默认为当前目录下.

#include( "one.txt" )

如果需要引入多个文件,可以像下面这样.

#include( "one.gif","two.txt","three.htm" )

当然,还可用一个变量名来代替文件名引入.

#include( "greetings.txt", $seasonalstock )

6.Parse(解析模板)

#parse 元素指示可以引入一个包含TVL的本地文件,这个文件将被Veloict engine解析输出。.

#parse( "me.vm" )

#include 指令不同, #parse 可以从引入的模板中得到变量引用.但#parse指令只能接受一个参数.

VTL templates 被#parse 的模板中还可以再包含#parse声明,默认的深度为10,这是由配置参数directive.parse.max.depth在文件velocity.properties中决定的,你可以修改它以适合项目要求

7.Stop

#stop 指令用来指示在模板的某处,engine停止解析,这一般用来调用。用法很简单.

#stop

8.Velocimacros(宏调用)

#macro 指令让模板设计者可以将些重复、相关的的脚本版断定义为一个功能块.无论在什么情况下. 出于单一意图设计的 Velocimacro都会最大程序的减少模板编写中可以的出错,还是看个例子来理解一下Velocimacros的概念.

#macro( d )

<tr><td></td></tr>

#end

这样就定义了一个名为d的宏,它可以在其它的模板中像下面那样直接引用:

#d()

Velocimacro可以接收0到任意多的传入参数.如上个例是0个参数,但当它被调用时,也必须传入同样多的参数. 这里定义了一个有两个参数的宏.

#macro( tablerows $color $somelist )

#foreach( $something in $somelist )

<tr><td bgcolor=$color>$something</td></tr>

#end

#end

然后,我们在页面中来使用:

#set( $greatlakes = ["Superior","Michigan","Huron","Erie","Ontario"] )

#set( $color = "blue" )

<table>

#tablerows( $color $greatlakes )

</table>

注意变量 $greatlakes 取代了宏中变量 $somelist的输出,最终的输出如下:

<table>

<tr><td bgcolor="blue">Superior</td></tr>

<tr><td bgcolor="blue">Michigan</td></tr>

<tr><td bgcolor="blue">Huron</td></tr>

<tr><td bgcolor="blue">Erie</td></tr>

<tr><td bgcolor="blue">Ontario</td></tr>

</table>

宏一般被定义在模板中,那么站点上的其它模板中又如何调用呢?如能定义一个可以更大范围内共想的宏就太好了

如果将宏#tablerows($color $list) 定义到一个模板库中(Velocimacros template library), 其它模板就都可以访问它了.

Velocimacro Arguments(宏的参数)

Velocimacros可以从TVL中接受以下参数 :

  • 引用类型 : 所有以'$'开头的

  • String literal : something like "$foo" or 'hello'

  • Number literal : 1, 2 etc

  • IntegerRange : [ 1..2] or [$foo .. $bar]

  • ObjectArray : [ "a", "b", "c"]

  • boolean value true

  • boolean value false

当传一个引用参数给宏时, 引用是通过名字传入的('pass by name').

#macro( callme $a )

$a $a $a

#end

#callme( $foo.bar() )

上例中命令 bar() 被调用了3 times.

最后要说的是,这个特性有些难以学习,但当你精心组织规划你的宏库时, 消除在VTL中重复功能的脚本时 –你可以像使用一个对象或组件一样使用宏, 比如一个宏对象生成多个表格的重复色彩.

如果你想利用这个特性,你只需要像下面那样简单的编码传一个值给它来调用 :

#set( $myval = $foo.bar() )

#callme( $myval )

Velocimacro Properties(宏的属性)

配置文件 velocity.properties 中有多行相关配置,具体请见《Velocity Java开发指南中文版》.

velocimacro.library –用来指定全局的宏库,多个可以,号分开.

velocimacro.permissions.allow.inline – 默认为true,可以让宏定义在一个正规的模板文件中.

velocimacro.permissions.allow.inline.to.replace.global – 用来指定模板内定义的宏的功能是否要以替换全局库,默认为false.

velocimacro.permissions.allow.inline.local.scope –模板中定义的宏的使用范围是否只是本模板可用.

velocimacro.context.localscope –如果为true,宏通过#set赋值时.宏中将保持一个,且不会由于context中的数据被修改而变化,同样,宏中的修改也不会改变context中的。

velocimacro.library.autoreload – 是否自动重新载入,用于调试环境,默认false,如为true,需要取掉chcheing:. file.resource.loader.cache = false ).

一些细节:

宏必须在模板中使用#macro()指令前定义.

尽量不要直接在模板中使用#parse() 包含 #macro() 指令.因为 #parse() 动作在运行时执行,时会有一个在VM中查找元素的过程.

13.注掉 VTL Directives

嗯,你学下英语吧,一看就懂J

VTL directives can be escaped with the backslash character ("\") in a manner similar to valid VTL references.

## #include( "a.txt" ) renders as <contents of a.txt>

#include( "a.txt" )

## \#include( "a.txt" ) renders as #include( "a.txt" )

\#include( "a.txt" )

## \\#include ( "a.txt" ) renders as \<contents of a.txt>

\\#include ( "a.txt" )

Extra care should be taken when escaping VTL directives that contain multiple script elements in a single directive (such as in an if-else-end statements). Here is a typical VTL if-statement:

#if( $jazz )

Vyacheslav Ganelin

#end

If $jazz is true, the output is

Vyacheslav Ganelin

If $jazz is false, there is no output. Escaping script elements alters the output. Consider the following case:

\#if( $jazz )

Vyacheslav Ganelin

\#end

Whether $jazz is true or false, the output will be

#if($ jazz )

Vyacheslav Ganelin

#end

In fact, because all script elements are escaped, $jazz is never evaluated for its boolean value. Suppose backslashes precede script elements that are legitimately escaped:

\\#if( $jazz )

Vyacheslav Ganelin

\\#end

In this case, if $jazz is true, the output is

\ Vyacheslav Ganelin

\

To understand this, note that the #if( arg ) when ended by a newline (return) will omit the newline from the output. Therefore, the body of the #if() block follows the first '\', rendered from the '\\' preceding the #if(). The last \ is on a different line than the text because there is a newline after 'Ganelin', so the final \\, preceding the #end is part of the body of the block.

If $jazz is false, the output is

\

Note that things start to break if script elements are not properly escaped.

\\\#if( $jazz )

Vyacheslave Ganelin

\\#end

Here the #if is escaped, but there is an #end remaining; having too many endings will cause a parsing error.

14.VTL: 一般使用的格式

尽量是这样:

#set( $imperial = ["Munetaka","Koreyasu","Hisakira","Morikune"] )

#foreach( $shogun in $imperial )

$shogun

#end

而不要这样写,虽然它可以运行:

Send #set($foo=["$10 and ","a pie"])#foreach($a in $foo)$a#end please.

Velocity会自动跳过空格,上面的可以这样写::

Send me

#set( $foo = ["$10 and ","a pie"] )

#foreach( $a in $foo )

$a

#end

please.

or as

Send me

#set($foo = ["$10 and ","a pie"])

#foreach ($a in $foo )$a

#end please.

In each case the output will be the same.

15.Other Features and Miscellany(其它特性和细节)

1.数学计算

如下是四则运算的例子:

#set( $foo = $bar + 3 )

#set( $foo = $bar - 4 )

#set( $foo = $bar * 6 )

#set( $foo = $bar / 2 )

2.Range Operator

The range operator can be used in conjunction with #set and #foreach statements. Useful for its ability to produce an object array containing integers, the range operator has the following construction:

[n..m]

Both n and m must either be or produce integers. Whether m is greater than or less than n will not matter; in this case the range will simply count down. Examples showing the use of the range operator as provided below:

First example:

#foreach( $foo in [1..5] )

$foo

#end

Second example:

#foreach( $bar in [2..-2] )

$bar

#end

Third example:

#set( $arr = [0..1] )

#foreach( $i in $arr )

$i

#end

Fourth example:

[1..3]

Produces the following output:

First example:

1 2 3 4 5

Second example:

2 1 0 -1 -2

Third example:

0 1

Fourth example:

[1..3]

Note that the range operator only produces the array when used in conjunction with #set and #foreach directives, as demonstrated in the fourth example.

Web page designers concerned with making tables a standard size, but where some will not have enough data to fill the table, will find the range operator particularly useful.

3.Advanced Issues: Escaping and !

When a reference is silenced with the ! character and the ! character preceded by an \ escape character, the reference is handled in a special way. Note the differences between regular escaping, and the special case where \ precedes ! follows it:

#set( $foo = "bar" )

$\!foo

$\!{foo}

$\\!foo

$\\\!foo

This renders as:

$!foo

$!{foo}

$\!foo

$\\!foo

Contrast this with regular escaping, where \ precedes $:

\$foo

\$!foo

\$!{foo}

\\$!{foo}

This renders as:

$foo

$!foo

$!{foo}

\bar

4.Velocimacro Miscellany(关于宏的一些问题)

这是一些简短的问题总结,也许你先要有这样一个概念:. 'Velocimacro' 就像一个‘VM’。

可否用一个指示符做为另外一个指示符运算的参数?

如 : #center( #bold("hello") )

No. 指示符不是有效的参数但你可以这样实现你想要的:

#set($stuff = "#bold('hello')" )

#center( $stuff )

或者:

#center( "#bold( 'hello' )" )

上面这个例子中,参数是在调用的宏中生成的.不是调用者传入的. 看看下面的例子 :

#macro( inner $foo )

inner : $foo

#end

#macro( outer $foo )

#set($bar = "outerlala")

outer : $foo

#end

#set($bar = 'calltimelala')

#outer( "#inner($bar)" )

输出将是:

Outer : inner : outerlala

因为 "#inner($bar)" 是发生在 #outer()内部的!

可否通过 #parse()来注册一个宏 ?

宏必须在模板使用前定义好.前面己有一个关于此问题的建议,#parse()是运行时执进的,JVM查找对象的顺序不一定会全按我们预计的执行。

什么是宏的自动重新装载?

这是由配置参数决定的, 为方例开发者,在生产环境中则不需要 :

velocimacro.library.autoreload

默认的是false.当设为true中,需要设定chcheing参数;

<type>.resource.loader.cache = false

(具体配置请见开发指南,如下是一个配置的例子)

file.resource.loader.path = templates

file.resource.loader.cache = false

velocimacro.library.autoreload = true

注意:Don't keep this on in production.

5.String Concatenation(连结字符串)

很简单,看例子就是 :

#set( $size = "Big" )

#set( $name = "Ben" )

The clock is $size$name.

上面的输出将是

'The clock is BigBen'.

或者:

#set( $size = "Big" )

#set( $name = "Ben" )

#set($clock = "$size$name" )

The clock is $clock.

它们都是同样的输出,最后一个例子如下,:

#set( $size = "Big" )

#set( $name = "Ben" )

#set($clock = "${size}Tall$name" )

The clock is $clock.

输出将是

'The clock is BigTallBen'.

分享到:
评论

相关推荐

    Velocity模板使用指南中文版

    《Velocity模板使用指南中文版》.rar

    《Velocity模板使用指南中文版》.doc

    《Velocity模板使用指南中文版》.doc。。。。。。。。。。。。。。。。。

    velocity模板使用指南中文版

    velocity是一个很实用的页面工具,相对于jsp更便于开发维护,本文档是他的一个基础使用工具,便于比更快学习使用velocity!

    velocity文档(Velocity1.4java开发指南中文版,Velocity1.4模板使用指南中文版中文版)

    Velocity1.4java开发指南中文版 Velocity1.4模板使用指南中文版中文版

    Velocity 模板使用指南

    Velocity的能力远不止web站点开发这个领域,例如,它可以从模板(template)产生SQL和PostScript、XML,它也可以被当 作一个独立工具来产生源代码和报告,或者作为其他系统的集成组件使用。 里面有架包、使用方法...

    velocity经典4pdf中文教程

    内容包括: 《Velocity模板使用指南中文版》 《VTL语法参考指南中文版》 《Velocity Web应用开发指南中文版》 《velocity Java开发指南中文版》

    velocity开发必备(中文指南+精通Velociy)

    velocity开发必备(中文指南+精通Velociy) 附件包含《Velocity模板使用指南中文版》和《精通Apache Velocity》

    Velocity 帮助文档大全(打包)

    本人搜集的Velocity相关的所有帮助文档,包括: 《Velocity Web应用开发指南中文版.pdf》、 《Velocity+Java开发指南中文版.pdf》、 《Velocity模板使用指南中文版.pdf》、 《VTL语法参考指南中文版.pdf》

    velocity学习文档

    《velocity Java开发指南中文版》 《Velocity Web应用开发指南中文版》 《Velocity模板使用指南中文版》

    《Velocity1.4 java开发指南》中文版

    1.基本使用模式 4 4.单实例还是多实例(To Singleton Or Not To Singleton...)? 6 1.Singleton Model 6 2.Separate Instance 6 5. The Context 7 1.The Basics 7 2.在模板中用#foreach指令支持迭代对象 8 3.Context ...

    velocity文档资源

    diyjava.com《Velocity模板使用指南中文版》.pdf Velocity+实现客户端和服务器端模板.doc velocity新手教程.doc Velocity指南.pdf

    Velocity1.4 学习指南中文版

    Velocity 的主要目标是通过模板生成格式文档. 因此, Velocity自身不提供任何Web相关的功能. 当你需要开发Web应用时, 需要一个框架来接收 HTTP请求和, 处理用户认证,执行业务逻辑调用,最后生成应答内容返回给客户端...

    Velocity学习指南(中文版).rar

    Velocity用户指南旨在帮助页面设计者和内容提供者了解Velocity和其简单而又强大的脚本语言(Velocity Template Language (VTL))。本指南中有很多示例展示了用Velocity来讲动态内容嵌入到网站之中,但是所有的VTL ...

    Velocity Java开发指南中文版

    Velocity 是一处基于java 语言的模板引擎, 使用这个简单、功能强大的开发工具,可以很容易的将数据对象灵活的 与格式化文档组装到一起;希望本文能指引使用velocity 在开发基于servlet 或一般java 应用程序的应用上...

    Velocity 资料汇总

    Velocity模板使用指南中文版 VTL语法参考指南中文版 DB4O系统应用之起步篇 velocity用户手册 velocity语法 velocity初探 velocity1.5帮助说明 velocity Tool1.4帮助说明 Veloctiy例子 Struts结合Velocity开发web应用...

    viocity学习资料

    内涵:Velocity_Java开发指南中文版,Velocity教程_天马行空,Velocity模板使用指南中文版,VTL语法参考指南中文版

Global site tag (gtag.js) - Google Analytics