VTL语法
一、引用
1.變量
格式:
$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ } ]
舉例:
Normal 格式: $mud-Slinger_9
Silent 格式: $!mud-Slinger_9
Formal 格式: ${mud-Slinger_9}
2.屬性
格式:
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[a..z, A..Z ][ a..z, A-Z, 0..9, -, _ ]* [ } ]
舉例:
Regular 格式: $customer.Address
Formal 格式: ${purchase.Total}
3.方法
格式:
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]*( [ opional parameter list... ] ) [ } ]
舉例:
Regular 格式: $customer.getAddress()
Formal 格式: ${purchase.getTotal()}
Regular 格式 with Parameter List: $page.setTitle( "My Home Page" )
二、指令
1.#set - 給引用賦值
格式:
#set( $ref = [ ", ' ]arg[ ", ' ] )
用法:
$ref - 左邊必須是一個變量引用或者屬性引用.
arg - 右邊的arg如果被加上雙引號則被解析, 如果是單引號則不會解析. 如果右邊的值為null,則應 該不會賦值給左邊
舉例:
變量引用: #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"] )
右邊也可以是簡單算術表達式, 例如:
加: #set( $value = $foo + 1 )
減: #set( $value = $bar - 1 )
乘: #set( $value = $foo * $bar )
除: #set( $value = $foo / $bar )
求余: #set( $value = $foo % $bar )
2.#if / #elseif / #else - output conditional on truth of statements
格式:
#if( [condition] ) [output] [ #elseif( [condition] ) [output] ]* [ #else [output] ] #end
用法:
condition - If a boolean, considered true if it has a true false; if not a boolean, considered true if not null.
output - May contain VTL.
舉例:
Equivalent Operator: #if( $foo == $bar )
Greater Than: #if( $foo > 42 )
Less Than: #if( $foo < 42 )
Greater Than or Equal To: #if( $foo >= 42 )
Less Than or Equal To: #if( $foo <= 42 )
Equals Number: #if( $foo == 42 )
Equals String: #if( $foo == "bar" )
Boolean NOT: #if( !$foo )
3.#foreach - 循環列值
格式:
#foreach( $ref in arg ) statement #end
用法:
$ref - 第一個變量引用.
arg - 可以是object array, collection, or map的引用。
statement -
引用: #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>
4. 默認計數器
在velocity.properties文件了有一個默認的循環計數器, 它是$velocityCount. 默認是從1開始計數,但是這個計數器的初值可以在velocity.properties文件里設置. 在velocity.properties如下設置:
# Default name of the loop counter
# refenence refernce.
counter.name = velocityCount
# Default starting value of the loop
# counter 變量引用.
counter.initial.value = 1
5.#include - 不被Velocity解析
格式:
#include( arg[, arg2, ... argn] )
arg -文件名
舉例:
字符串: #include( "disclaimer.txt", "opinion.txt" )
變量: #include( $foo, $bar )
6.#parse - 被Velocity解析
格式:
#parse( arg )
arg - 文件名
舉例:
字符串: #parse( "lecorbusier.vm" )
變量: #parse( $foo )
7.#stop - 停止模板引擎
格式:
#stop
用法:
將會停止當前模板引擎的執行,這是一個很好的調試。
8.#macro - 定義一個Velocity宏
所有的用戶都可以定義一個Velocity宏,允許模板設計者定義一段可重用的VTL template
格式:
#macro( vmname $arg1 [ $arg2 $arg3 ... $argn ] ) [ VM VTL code... ] #end
例如:
#macro ( d )
<tr><td></td></tr>
#end
在上面的例子中Velocimacro被定義為d,然后你就可以在任何VTL directive中以如下方式調用它:
#d()
當你的template被調用時,Velocity將用<tr><td></td></tr>替換為#d()。
例如2:帶參數的宏
#macro ( tablerows $color $somelist )
#foreach ( $something in $somelist )
<tr><td bgcolor=$color>$something</td</tr>
#end
#end
調用#tablerows Velocimacro:
#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>
三、注釋
1. 單行注釋 ##
例如:
## This is a comment.
2. 多行注釋
Example:
#*
This is a multiline comment.
This is the second line
*#
轉載于:https://www.cnblogs.com/zhuhc/archive/2013/05/04/3059489.html
總結
- 上一篇: Android动画之仿美团加载数据等待时
- 下一篇: 个人微信小程序云开发总结心得