VTL语法参考
更多請參考:http://velocity.apache.org/engine/devel/user-guide.html
1.變量定義
變量名的有效字符集:
$?[?!?][?{?][?a..z,?A..Z?][?a..z,?A..Z,?0..9,?-,?_?][?}?]
Examples:
- 一般方式: $mud-Slinger_9
- 靜態(tài)(輸出原始字面): $!mud-Slinger_9
- 正規(guī)格式: ${mud-Slinger_9}
2.訪問屬性
格式規(guī)則:
$?[?{?][?a..z,?A..Z?][?a..z,?A..Z,?0..9,?-,?_?]*?.[a..z,?A..Z?][?a..z,?A-Z,?0..9,?-,?_?]* [?}?]
Examples:
- 一般格式: $customer.Address :調(diào)用customer對象的getAddress()命令.
- 正規(guī)格式: ${purchase.Total}
3.命令調(diào)用
格式規(guī)則:
$?[?{?][?a..z,?A..Z?][?a..z,?A..Z,?0..9,?-,?_?]*?.[?a..z,?A..Z?][?a..z,?A..Z,?0..9,?-,?_?]*(?[?optional parameter list...?]?)?[?}?]
Examples:
- 一般寫碼: $customer.getAddress()
- 正規(guī)寫法: ${purchase.getTotal()}
- 傳入調(diào)用參數(shù): $page.setTitle( "My Home Page" )
VTL的屬性調(diào)用可以理解為命令調(diào)用的簡寫方式,一般會調(diào)用對象的get/set命令.
3.動作指令
1.#set – 建立變量對值的引用
格式規(guī)則:
#?[?{?]?set?[?}?]?( $ref?=?[?",?'?]arg[?",?'?] )
Examples:
- 變量引用: #set( $monkey = $bill )
- 引用原始字符串: #set( $monkey.Friend = 'monica' )
- 屬性引用: #set( $monkey.Blame = $whitehouse.Leak )
- 命令引用: #set( $monkey.Plan = $spindoctor.weave($web) )
直接引用數(shù)字: #set( $monkey.Number = 123 )
- 列表賦值引用: #set( $monkey.Numbers = [1..3] )
- 對象數(shù)組: #set( $monkey.Say = ["Not", $my, "fault"] )
右值也可以做為一個表達式出現(xiàn),如下加,減,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 )
2.#if/#elseif/#else-條件判斷
格式規(guī)則:
#?[?{?]?if?[?}?]?(?[條件表達式]?)?[輸出內(nèi)容] [?#?[?{?]?elseif?[?}?]?(?[condition]?)?[output] ]* [?#?[?{?]?else?[?}?] [output] ]?#?[?{?]?end?[?}?]
Usage:
- condition?– 如果是boolean型,根據(jù)true或false決定,否則非null時認為是true.
- output?–可以包含VTL的輸出內(nèi)容.
Examples (showing different operators):
Operator Name
Symbol
Alternative Symbol
Example
Equals Number
==
eq
#if( $foo == 42 )
Equals String
==
eq
#if( $foo == "bar" )
Object Equivalence
==
eq
#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.????? “== “操作可以用來比較數(shù)字,字符串,或同一個類的不同對象或不同類型的對象. 當是不同類的對象時,會調(diào)用它們的toString()命令結(jié)果來做比較看是否相等.
2.????? 也可以如下用法,但注意else處,用{}括起.
#if( $foo == $bar)it's true!#{else}it's not!#end</li>
3.#foreach---使用循環(huán)通過列表迭代對象
Format:
#?[?{?]?foreach?[?}?]?(?$ref?in?arg?)?statement?#?[?{?]?end?[?}?]
Usage:
- $ref?– 引用的要迭代的對象.
- arg?– 可能是:一個列表引用 (i.e. object array, collection, or map), an array list, 或其它列表.
- statement?– 當velocity發(fā)現(xiàn)下一個有效對像在列表中,輸出可以是一個合法的VTL.
示例 #foreach()用法,:
- 引用: #foreach ( $item in $items )
- 數(shù)組列表: #foreach ( $item in ["Not", $my, "fault"] )
- 根據(jù)設(shè)定的界限: #foreach ( $item in [1..3] )
如下可以取得循環(huán)次數(shù)的當前值:
<table>
#foreach( $customer in $customerList )
??? <tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
</table>
默認的循環(huán)次數(shù)的引用變量名為 $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
注意,可以對所有可循環(huán)的次數(shù)加一個最大值來控制,默認的是-1,表示元限制:
# The maximum allowed number of loops.
directive.foreach.maxloops = -1
4.#include – 在模板中引入本地文件,不用Velocity解析這個文件
Format:
#?[?{?]?include?[?}?]?(?arg[ arg2 ... argn]?)
- arg?– 目錄TEMPLATE_ROOT下面的有效文件名.
Examples:
- 直接寫文件名: #include( "disclaimer.txt,"opinion.txt" ):如有多個文件時用逗號分開
- 使用變量引用的文件名: #include( $foo,$bar )
5.#parse – 在模板引用處使用Velocity解析另一個模板輸出
Format:
#?[?{?]?parse?[?}?]?(?arg?)
- arg?-目錄TEMPLATE_ROOT下面的有效文件名.
Examples:
- 直接寫文件名: #parse( "lecorbusier.vm" )
- 使用變量引用的文件名: #parse( $foo )
通過設(shè)置配置中的解析層次深度的最大值velocity.properties中項?parse_directive.maxdepth?in可以防止死循環(huán). (The default parse depth is 10.)
6.#stop – 中斷模板解析
Format:
#?[?{?]?stop?[?}?]
Usage:
在當前模板指令處停止解析,為方便調(diào)試用.
7.#macro – 讓用戶可以定義宏操作(Velocimacro (VM):一組實現(xiàn)特定功能的VTL)
Format:
#?[?{?]?macro?[?}?]?(?vmname $arg1 [ $arg2 $arg3 ... $argn ]?)?[ VM VTL code... ]?#?[?{?]?#end?[?}?]
- vmname?– 宏名字 VM (#vmname)
- $arg1 $arg2 [ ... ]?– 要傳給宏的參數(shù)VM..
- [ VM VTL code... ]?–宏代碼,有效的VTL.
一次定義好了,就可以在其它模板的任何地方使用宏指令來應(yīng)用.
#vmname( $arg1 $arg2 )
宏(VM)可以寫在以下兩個地方:
1.??????(模板庫)Template library:?可以配置用戶定義的庫以便全站使用
2.??????Inline:?放入到一般的模板文件中, 僅當配置參數(shù)?velocimacro.permissions.allowInline=true?時生效.
4.Comments 注解
Comments不是運行時所必須的,但你一定要寫.
1.單行注解
Example:
## This is a comment.
2.多行注解
Example:
#*
This is a multiline comment.
This is the second line
*#
本文轉(zhuǎn)自 netcorner 博客園博客,原文鏈接:http://www.cnblogs.com/netcorner/archive/2008/07/11/2912125.html?? ,如需轉(zhuǎn)載請自行聯(lián)系原作者
總結(jié)
- 上一篇: 面向初学者的高阶组件介绍
- 下一篇: python 爬取智联招聘