条件语句(#if, #elseif, #else , #end)
当一个web页面被生成时如果使用了Velocity的#if 指令,那么当条件成立的时,执行相应的代码。例如:
2 | <strong>Velocity!</strong> |
上例中的条件语句将在满足以下两种条件之一时成立:
- $foo是一个boolean型的变量,且它的值为true
- $foo变量的值不为null
这里需要注意一点, Velocity context仅仅能够包含对象,所以当我们说“boolean”时实际上代表的时一个Boolean对象。即便某个方法返回的是一个boolean值,Velocity也会利用内省机制将它转换为一个Boolean的相同值。
#elseif和#else元素可以同#if一同使用。例如:
2 | <strong> Go North </strong> |
4 | <strong> Go East </strong> |
6 | <strong> Go South </strong> |
8 | <strong> Go West </strong> |
注意,这里的Velocity的数字是作为Integer来比较的, 其他类型的对象将使得条件为false,但是与java不同的是Velocity使用“==”来比较两个值,而且velocity要求等号两边的值类型相同。
关系、逻辑运算符
Velocity中使用双等号(==) 判断两个变量的关系。例如:
1 | #set ( $foo = “deoxyribonucleic acid” ) |
2 | #set ( $bar = “ribonucleic acid” ) |
4 | In this case it’s clear they aren’t equivalent.So… |
6 | They are not equivalent and this will be the output. |
Velocity有AND、OR和NOT逻辑运算符。例子:
3 | <strong> This AND that </strong> |
3 | <strong>This OR That </strong> |
3 | <strong> NOT that </strong> |
循环(#foreach)
例子:
2 | #foreach ( $product in $allProducts ) |
每次循环都从$allProducts中取出一个值赋给$product变量。
$allProducts可以是一个Vector、Set或者Array。分配给$product的值是一个java对象,并且可以通过变量被引用。 如果$product是一个java的Product类,并且这个产品的名字可以通过调用他的getName()方法得到。
现在我们假设$allProducts是一个Hashtable,如果你希望得到它的key应该像下面这样:
2 | #foreach ( $key in $allProducts.keySet() ) |
3 | <li>Key: $key -> Value: $allProducts.get($key) </li> |
Velocity还特别提供了得到循环次数的方法
:
2 | #foreach ( $customer in $customerList ) |
3 | <tr><td>$velocityCount</td><td>$customer.Name</td></tr> |
$velocityCount变量的名字是Velocity内置的变量。你也可以通过修改velocity.properties文件来改变它。默认情况下,计数从“1”开始,但是你可以在velocity.properties设置它是从“1”还是从“0”开始。下面的设置将velocityCount的名字改为loopCount, 循环计数从0开始:
3 | directive.foreach.counter.name = velocityCount |
7 | directive.foreach.counter.initial.value = 1 |
#include指令
#include指令允许模板设计者引入本地文件, 被引入文件的内容将不会通过模板引擎被render。为了安全的原因,被引入的本地文件只能在TEMPLATE_ROOT目录下。
#include指令的用法很简单:
如果您需要引入多个文件,可以用逗号分隔就行:
1 | #include ("one.gif", "two.txt", "three.htm") |
在括号内可以是文件名,可以使用变量的:
1 | #inclue ("greetings.txt", $seasonalstock ) |
#parse指令
#parse 指令允许模板设计者一个包含VTL的本地模板文件, Velocity将解析其中的VTL并进行渲染。用法
#parse指令用法:
和#include一样,#parse指向的模板都必须包含在TEMPLATE_ROOT目录下, 但#parse只能指定单个对象。
#parse是可以递归调用的,例如在foo.vmz中有如下内容:
3 | #parse ( “parsefoo.vm” ) |
4 | All done with dofoo.vm! |
那么在parsefoo.vm模板中,你可以包含如下VTL:
2 | #set ( $count = $count – 1 ) |
4 | #parse( “parsefoo.vm” ) |
6 | All done with parsefoo.vm! |
渲染后的结果为:
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用法
Velocity的 宏(#macro)
#macro指令用于定义Velocity的宏。 宏可以定义一段可重用的VTL template。例如:
上面的例子中定义了一个Velocity的宏, 然后你可以这样来使用它:
当你的template被调用时,Velocity将用<tr><td></td></tr>替换为#d()。
每个Velocity的宏可以拥有任意数量的参数。虽然定义时可以随意设置参数数量,但是调用这个Velocimacro时必须指定正确的参数。下面是一个拥有两个参数的宏,其中一个参数是color,另一个参数是array:
01 | #macro ( tablerows $color $somelist ) |
02 | #foreach ( $something in $somelist ) |
03 | <tr><td bgcolor=$color>$something</td</tr> |
06 | #set ( $greatlakes = [ “Superior”, “Michigan”, “Huron”, “Erie”, “Ontario” ] ) |
07 | #set ( $color = “blue” ) |
09 | #tablerows( $color $greatlakes ) |
渲染后:
<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
当将一个reference作为参数传递给Velocity macro时,请注意reference作为参数时是以名字的形式传递的。这就意味着参数的值在每次Velocity macro内执行时才会被产生(相当于某些个特性使得你可以将一个方法调用作为参数传递给Velocimacro,而每次Velocimacro执行时都是通过这个方法调用产生不同的值来执行的。例如:
执行的结果是:reference $foo的bar()方法被执行了三次。
如果你不需要这样的特性, 可以通过以下方法:
1 | #set ( $myval = $foo.bar() ) |
转自:
http://my.oschina.net/aiguozhe/blog/39556