1. Velocity中僅有String可以被賦值給變量。
2. 單行註釋:## This is a single line comment.
3. 多行註釋:
#*
Thus begins a multi-line comment. Online visitors won't
see this text because the Velocity Templating Engine will
ignore it.
*#
4. $customer.Address有兩種含義。它可以表示:查找hashtable對象customer中以Address爲關鍵字的值;也可以表示調用customer對 象的getAddress()方法。
5. reference的正是格式如下:
${mudSlinger} 變量
${customer.Address} 屬性
${purchase.getAddress()} 方法 和屬性一樣,但是方法可以指定參數,而屬性不能。
6. VTL中的reference總是以一個大寫或者小寫的字母開始。
7. VTL中使用"\"作爲逃逸符。
8. 注意:VTL中未被定義的變量將被認爲是一個字符串,
9. 賦值左側的(LHS)必須是一個變量或者屬性reference。
右側(RHS)可以是以下類型中一種:
l 變量reference
l String literal
l 屬性reference
l 方法reference
l number literal
l ArrayList
10. 如果你的RHS是一個null,VTL的處理將比較特殊:它將指向一個已經存在的reference,這對初學者來講可能是比較費解的。
11. Velocity有AND、OR和NOT邏輯運算符。
#if( $foo && $bar )
#if ( $foo || $bar )
#if ( !$foo )
12. Foreach循環 :
#foreach ( $product in $allProducts )
<li> $product </li>
#end
如果$product是一個java的Product類,並且這個產品的名字可以通過調用他的getName()方法得到。
現在我們假設$allProducts是一個Hashtable,如果你希望得到它的key應該像下面這樣:
<ul>
#foreach ( $key in $allProducts.keySet() )
<li>Key: $key -> Value: $allProducts.get($key) </li>
#end
</ul>
13. Velocity還特別提供了得到循環次數的方法:
<table>
#foreach ( $customer in $customerList )
<tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
</table>
$velocityCount變量的名字是Velocity默認的名字,你也可以通過修改velocity.properties文件來改變它。默認情況下,計數從"1"開始,但是你可以在velocity.properties設置它是從"1"還是從"0"開始。
13. include
#include script element允許模板設計者引入本地文件。被引入文件的內容將不會通過模板引擎被render。爲了安全的原因,被引入的本地文件只能在TEMPLATE_ROOT目錄下。
#inclued ( "one.txt" )
如果您需要引入多個文件,可以用逗號分隔就行:
#include ( "one.gif", "two.txt", "three.htm" )
在括號內可以是文件名,但是更多的時候是使用變量的:
#inclue ( "greetings.txt", $seasonalstock )
14. parse
#parse script element允許模板設計者一個包含VTL的本地文件。Velocity將解析其中的VTL並render模板。
#parse( "me.vm" )任何由#parse指向的模板都必須包含在TEMPLATE_ROOT目錄下。與#include不同的是,#parse只能指定單個對象。
你可以通過修改velocity.properties文件的parse_direcive.maxdepth的值來控制一個template可以包含的最多#parse的個數――默 認值是10。
15. Stop:
#stop script element允許模板設計者停止執行模板引擎並返回。把它應用於debug是很有幫助
e.g: #stop
16. Velocimacros
#macro script element允許模板設計者定義一段可重用的VTL template。例如:
#macro ( d )
<tr><td></td></tr>
#end
在上面的例子中Velocimacro被定義爲d,然後你就可以在任何VTL directive中以如下方式調用它:
#d()
17. Velocimacro,一個參數是color另一個參數是array:
#macro ( tablerows $color $somelist )
#foreach ( $something in $somelist )
<tr><td bgcolor=$color>$something</td</tr>
#end
#end