当一个web页面被生成时如果使用了Velocity的#if 指令,那么当条件成立的时,执行相应的代码。例如:
#if ( $foo )
<strong>Velocity!</strong>
#end
上例中的条件语句将在满足以下两种条件之一时成立:
- $foo是一个boolean型的变量,且它的值为true
- $foo变量的值不为null
#elseif和#else元素可以同#if一同使用。例如:
#if( $foo < 10 )
<strong> Go North </strong>
#elseif( $foo == 10 )
<strong> Go East </strong>
#elseif( $foo == 6 )
<strong> Go South </strong>
#else
<strong> Go West </strong>
#end
注意,这里的Velocity的数字是作为Integer来比较的, 其他类型的对象将使得条件为false,但是与java不同的是Velocity使用“==”来比较两个值,而且velocity要求等号两边的值类型相同。
关系、逻辑运算符
Velocity中使用双等号(==) 判断两个变量的关系。例如:#set ( $foo = “deoxyribonucleic acid” )
#set ( $bar = “ribonucleic acid” )
#if ( $foo == $foo )
In this case it’s clear they aren’t equivalent.So…
#else
They are not equivalent and this will be the output.
#end
Velocity有AND、OR和NOT逻辑运算符。例子:
## logical AND
#if( $foo && $bar )
<strong> This AND that </strong>
#end
## logical OR
#if ( $foo || $bar )
<strong>This OR That </strong>
#end
##logical NOT
#if ( !$foo )
<strong> NOT that </strong>
#end
循环(#foreach)
例子:
<ul>
#foreach ( $product in $allProducts )
<li> $product </li>
#end
</ul>
每次循环都从$allProducts中取出一个值赋给$product变量。 $allProducts可以是一个Vector、Set或者Array。分配给$product的值是一个java对象,并且可以通过变量被引用。 如果$product是一个java的Product类,并且这个产品的名字可以通过调用他的getName()方法得到。 现在我们假设$allProducts是一个Hashtable,如果你希望得到它的key应该像下面这样:
<ul>
#foreach ( $key in $allProducts.keySet() )
<li>Key: $key -> Value: $allProducts.get($key) </li>
#end
</ul>
Velocity还特别提供了得到循环次数的方法 :
<table>
#foreach ( $customer in $customerList )
<tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
</table>
$velocityCount变量的名字是Velocity内置的变量。你也可以通过修改velocity.properties文件来改变它。默认情况下,计数从“1”开始,但是你可以在velocity.properties设置它是从“1”还是从“0”开始。下面的设置将velocityCount的名字改为loopCount, 循环计数从0开始:
# Default name of loop counter
# variable reference
directive.foreach.counter.name = velocityCount
# Default starting value of the loop
# counter variable reference
directive.foreach.counter.initial.value = 1
#include指令
#include指令允许模板设计者引入本地文件, 被引入文件的内容将不会通过模板引擎被render。为了安全的原因,被引入的本地文件只能在TEMPLATE_ROOT目录下。
#include指令的用法很简单:
#inclued ("one.txt")
如果您需要引入多个文件,可以用逗号分隔就行:
#include ("one.gif", "two.txt", "three.htm")
在括号内可以是文件名,可以使用变量的:
#inclue ("greetings.txt", $seasonalstock )
#parse指令
#parse 指令允许模板设计者一个包含VTL的本地模板文件, Velocity将解析其中的VTL并进行渲染。用法
#parse指令用法:
#parse("me.vm")
和#include一样,#parse指向的模板都必须包含在TEMPLATE_ROOT目录下, 但#parse只能指定单个对象。
#parse是可以递归调用的,例如在foo.vmz中有如下内容:
Count down.
#set ( $count = 8 )
#parse ( “parsefoo.vm” )
All done with dofoo.vm!
那么在parsefoo.vm模板中,你可以包含如下VTL:
$count
#set ( $count = $count – 1 )
#if ( $count > 0 )
#parse( “parsefoo.vm” )
#else
All done with parsefoo.vm!
#end
渲染后的结果为:
Count down.
8
7
6
5
4
3
2
1
0
All done with parsefoo.vm!
All done with dofoo.vm!
你可以通过修改velocity.properties文件的parse_direcive.maxdepth属性值来控制嵌套#parse的层数,默认值是10。
#stop指令
#stop指令允许模板设计者停止执行模板引擎并返回。#stop对调试很有帮助。#stop用法
#stop
Velocity的 宏(#macro)
#macro指令用于定义Velocity的宏。 宏可以定义一段可重用的VTL template。例如:
#macro ( d )
<tr><td></td></tr>
#end
上面的例子中定义了一个Velocity的宏, 然后你可以这样来使用它:
#d()
当你的template被调用时,Velocity将用<tr><td></td></tr>替换为#d()。
每个Velocity的宏可以拥有任意数量的参数。虽然定义时可以随意设置参数数量,但是调用这个Velocimacro时必须指定正确的参数。下面是一个拥有两个参数的宏,其中一个参数是color,另一个参数是array:
#macro ( tablerows $color $somelist )
#foreach ( $something in $somelist )
<tr><td bgcolor=$color>$something</td</tr>
#end
#set ( $greatlakes = [ “Superior”, “Michigan”, “Huron”, “Erie”, “Ontario” ] )
#set ( $color = “blue” )
<table>
#tablerows( $color $greatlakes )
</table>
渲染后:
<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>
Velocity的宏可以在Velocity模板内实现行内定义(inline)。 这就意味着同一个web site内的其他Velocity模板不可以获得该宏的定义。
定义一个可以被所有模板共享的Velocimacro显然是有很多好处的:它减少了在一大堆模板中重复定义的数量、节省了工作时间、减少了出错的几率、保证了单点修改。
实际上,上面定义的#tablerows( $color $list ) 宏已被定义在一个Velocity宏模板库(在velocity.properties中定义)里,所以这个macro可以在任何规范的模板中被直接调用。
Velocity的宏可以使用以下任何元素作为参数:
- 任何以$开头的reference
- 字符串字面值(String literal)
- 数字字面值(Number literal)
- 整数范围(IntegerRange):[1….3] , [$foo….$bar]
- 数组列表(Array List):[“a”,”b”,”c”]
- 布尔字面值(Boolean Literal):true、false
#macro ( callme $a )
$a $a $a
#end
#callme( $foo.bar() )
执行的结果是:reference $foo的bar()方法被执行了三次。 如果你不需要这样的特性, 可以通过以下方法:
#set ( $myval = $foo.bar() )
#callme ( $myval )