< DOCTYPE html PUBLIC -WCDTD XHTML TransitionalEN httpwwwworgTRxhtmlDTDxhtml-transitionaldtd>
變量名的有效字元集:
$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ } ]
Examples:
一般方式: $mud-Slinger_9
靜态(輸出原始字面): $!mud-Slinger_9
正規格式: ${mud-Slinger_9}
格式規則:
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[a..z, A..Z ][ a..z, A-Z, 0..9, -, _ ]* [ } ]
一般格式: $customer.Address :調用customer對象的getAddress()指令.
正規格式: ${purchase.Total}
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]*( [ optional parameter list... ] ) [ } ]
一般寫碼: $customer.getAddress()
正規寫法: ${purchase.getTotal()}
傳入調用參數: $page.setTitle( "My Home Page" )
VTL的屬性調用可以了解為指令調用的簡寫方式,一般會調用對象的get/set指令.
# [ { ] set [ } ] ( $ref = [ ", ' ]arg[ ", ' ] )
變量引用: #set( $monkey = $bill )
引用原始字元串: #set( $monkey.Friend = 'monica' )
屬性引用: #set( $monkey.Blame = $whitehouse.Leak )
指令引用: #set( $monkey.Plan = $spindoctor.weave($web) )
直接引用數字: #set( $monkey.Number = 123 )
清單指派引用: #set( $monkey.Numbers = [1..3] )
對象數組: #set( $monkey.Say = ["Not", $my, "fault"] )
右值也可以做為一個表達式出現,如下加,減,cheng,除和取模:
Addition: #set( $value = $foo + 1 )
Subtraction: #set( $value = $bar - 1 )
Multiplication: #set( $value = $foo * $bar )
Division: #set( $value = $foo / $bar )
Remainder: #set( $value = $foo % $bar )
# [ { ] if [ } ] ( [條件表達式] ) [輸出内容] [ # [ { ] elseif [ } ] ( [condition] ) [output] ]* [ # [ { ] else [ } ] [output] ] # [ { ] end [ } ]
Usage:
condition – 如果是boolean型,根據true或false決定,否則非null時認為是true.
output –可以包含VTL的輸出内容.
Examples (showing different operators):
Operator Name
Symbol
Alternative Symbol
Example
Equals Number
==
eq
#if( $foo == 42 )
Equals String
#if( $foo == "bar" )
Object Equivalence
#if( $foo == $bar )
Not Equals
!=
ne
#if( $foo != $bar )
Greater Than
>
gt
#if( $foo > 42 )
Less Than
<
lt
#if( $foo < 42 )
Greater Than or Equal To
>=
ge
#if( $foo >= 42 )
Less Than or Equal To
<=
le
#if( $foo <= 42 )
Boolean NOT
!
not
#if( !$foo )
注意:
1. “== “操作可以用來比較數字,字元串,或同一個類的不同對象或不同類型的對象. 當是不同類的對象時,會調用它們的toString()指令結果來做比較看是否相等.
2. 也可以如下用法,但注意else處,用{}括起.
#if( $foo == $bar)it's true!#{else}it's not!#end</li>
Format:
# [ { ] foreach [ } ] ( $ref in arg ) statement # [ { ] end [ } ]
$ref – 引用的要疊代的對象.
arg – 可能是:一個清單引用 (i.e. object array, collection, or map), an array list, 或其它清單.
statement – 當velocity發現下一個有效對像在清單中,輸出可以是一個合法的VTL.
示例 #foreach()用法,:
引用: #foreach ( $item in $items )
數組清單: #foreach ( $item in ["Not", $my, "fault"] )
根據設定的界限: #foreach ( $item in [1..3] )
如下可以取得循環次數的目前值:
<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
注意,可以對所有可循環的次數加一個最大值來控制,預設的是-1,表示元限制:
# The maximum allowed number of loops.
directive.foreach.maxloops = -1
# [ { ] include [ } ] ( arg[ arg2 ... argn] )
arg – 目錄TEMPLATE_ROOT下面的有效檔案名.
直接寫檔案名: #include( "disclaimer.txt,"opinion.txt" ):如有多個檔案時用逗号分開
使用變量引用的檔案名: #include( $foo,$bar )
# [ { ] parse [ } ] ( arg )
arg -目錄TEMPLATE_ROOT下面的有效檔案名.
直接寫檔案名: #parse( "lecorbusier.vm" )
使用變量引用的檔案名: #parse( $foo )
通過設定配置中的解析層次深度的最大值velocity.properties中項 parse_directive.maxdepth in可以防止死循環. (The default parse depth is 10.)
# [ { ] stop [ } ]
在目前模闆指令處停止解析,為友善調試用.
# [ { ] macro [ } ] ( vmname $arg1 [ $arg2 $arg3 ... $argn ] ) [ VM VTL code... ] # [ { ] #end [ } ]
vmname – 宏名字 VM (#vmname)
$arg1 $arg2 [ ... ] – 要傳給宏的參數VM..
[ VM VTL code... ] –宏代碼,有效的VTL.
一次定義好了,就可以在其它模闆的任何地方使用宏指令來應用.
#vmname( $arg1 $arg2 )
宏(VM)可以寫在以下兩個地方:
1. (模闆庫)Template library: 可以配置使用者定義的庫以便全站使用
2. Inline: 放入到一般的模闆檔案中, 僅當配置參數 velocimacro.permissions.allowInline=true 時生效.
Comments不是運作時所必須的,但你一定要寫.
Example:
## This is a comment.
#*
This is a multiline comment.
This is the second line
*#
本文轉自 netcorner 部落格園部落格,原文連結:<b>http://www.cnblogs.com/netcorner/archive/2008/07/11/2912125.html</b> ,如需轉載請自行聯系原作者