1. 程式人生 > >Velocity語法命令介紹

Velocity語法命令介紹

Velocity是一個基於Java的模板引擎(template engine)。它允許任何人僅僅簡單的使用模板語言(template language)來引用由java程式碼定義的物件。

#set($maxValue=5)
#set($name="Bob")
#set($arrayName=["element1","element2",...])
## This is a single line comment.
#*
Thus begins a multi-line comment. Online visitors won't
see this text because the Velocity Templating Engine will
ignore it.
*#

Velocity 遇到一個不能處理的引用時,一般他會直接輸出這個引用$email 的寫法,頁面上會看到的是$email,如下例,我們可以在$後面加上一個!號,那麼就會輸出空白:

<input type="text" name="email" value="$email"/>
<input type="text" name="email" value="$!email"/>

轉義字元

如果email 己定義了(比如它的值是foo),而這裡你卻想輸出$email. 這樣一個字串,就需要使用轉義字元”\”.

## The following line defines $email in this template:
#set( $email = "foo" )
$email
\$email
\\$email
\\\$email

上面的模板在web 頁面上的輸出將是:

foo
$email
\foo
\$email

但注意:如果右邊的運算元是一個屬性或命令的引用而返回null,那麼賦值將不會成功,且在隨後也不能再取出使用. 如下例:

#set( $result = $query.criteria("name"))
The result of the first query is $result
#set( $result = $query.criteria("address"))
The result of the second query is $result

如果$query.criteria("name") 返回的是字串"bill", 但$query.criteria("address") 返回null,上面的輸出結果將是:

The result of the first query is bill
The result of the second query is bill

又如下例:

#set( $criteria = ["name", "address"] )
#foreach($criterion in $criteria )
    #set($result=$query.criteria($criterion) )
    #if($result)
        Query was successful
#end

在上例中,就不能依賴if($result)來決定查詢是否成功. #set右邊如果是null會它將不能被賦其它值.一個解決辦法是,每次都將$result設為false:

#set( $criteria = ["name", "address"] )
#foreach($criterion in $criteria )
    #set($result = false)
    #set($result=$query.criteria($criterion) )
    #if($result)
        Query was successful
#end

條件判斷
#if($foo<10)
    <strong>Go North</strong>
#elseif($foo==10)
    <strong>Go East</strong>
#elseif($bar==6)
    <strong>Go South</strong>
#else
    <strong>Go West</strong>
#end

迴圈

通過引用變數$velocityCount 可以訪問到Velocity 提供的計數器:

<table>
#foreach( $customer in $customerList )
    <tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
</table>

$velocityCount是預設的計數器引用,你可以在配置velocity.properties 中改成你喜歡的:

include指令碼元素讓模板設計者可以在模板中引入一個本地檔案, 這個被引入的檔案將不會經過Velocity 的解析. 安全起見,可以引放的檔案只是是配置引數TEMPLATE_ROOT所定義目錄下的,預設為當前目錄下.

#include( "one.txt" )

多個檔案或者用變數名代替:

#include( "greetings.txt", $seasonalstock )

parse元素指示可以引入一個包含TVL 的本地檔案,這個檔案將被Velocity engine 解析輸出。

#parse( "me.vm" )

與#include 指令不同, #parse 可以從引入的模板中得到變數引用.但#parse 指令只能接受一個引數.

VTL templates 被#parse 的模板中還可以再包含#parse 宣告,預設的深度為10,這是由配置引數directive.parse.max.depth 在檔案velocity.properties 中決定的,你可以修改它以適合專案要求.

stop指令用來指示在模板的某處,engine 停止解析,這一般用來呼叫。用法很簡單.

#stop

macro指令讓模板設計者可以將些重複、相關的的指令碼片段定義為一個功能塊.

#macro( tablerows $color $somelist )
#foreach( $something in $somelist )
    <tr><td bgcolor=$color>$something</td></tr>
#end
#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>

如果將巨集#tablerows($color $list) 定義到一個模板庫中(Velocimacros template library), 其它模板就都可以訪問它了.

儘量不要直接在模板中使用#parse() 包含#macro() 指令.因為#parse() 動作在執行時執行時會有一個在VM 中查詢元素的過程.