Velociy 1.6新增了一些非常有用的指令和功能,并修復(fù)了一些bug,用起來的確方便多了。這里我將列舉一些我們能夠直觀感受得到的一些改變.
TYPE
|
CHANGE |
eg.
|
ADD
|
增加#define指令 定義可以引用的VTL渲染塊
|
#define( $foo )Hello, $bar!#end
#set( $bar = 'world')
$foo 輸出: Hello, world! |
ADD
|
增加可以跳出#foreach循環(huán)的#break指令 |
#foreac($a in $list)
#if($a==1)
#break
#end
#end
|
ADD |
增加#foreach指令中使用的VTL語法變量 $velocityHasNext判斷是否還有下一個元素 |
#foreach( $customer in $customerList )
$customer.Name #if( $velocityHasNext ), #end
#end
配置:
directive.foreach.counter.name = velocityCount
directive.foreach.iterator.name = velocityHasNext
|
ADD
|
允許模板中使用數(shù)組時當(dāng)做List來使用,意味著可以使用list的方法 |
$myarray.isEmpty()
$myarray.size()
$myarray.get(2)
$myarray.set(1, 'test')
|
ADD
|
支持模板上調(diào)用變參數(shù)方法 |
對于public void setPlanets(String... planets)
或者 public void setPlanets(String[] planets)
模板上可以這樣調(diào)用:
$sun.setPlanets('Earth', 'Mars', 'Neptune')
$sun.setPlanets('Mercury')
$sun.setPlanets()
|
ADD |
增加嚴(yán)格引用檢查設(shè)置,必須在#set或者context中設(shè)置的變量才能使用,否則會拋出exception(可配置開啟關(guān)閉) |
如果$foo不存在,并且嚴(yán)格引用檢查開啟
$foo ## Exception
#set($bar = $foo) ## Exception
#if($foo == $bar)#end ## Exception
#foreach($item in $foo)#end ## Exception
|
ADD |
增加模板上定義map
|
#set( $monkey.Map = {"banana" : "good", "roast beef" : "bad"}) ## Map
使用:
$monkey.Map.get("banana")
或 $monkey.Map.banana
|
ADD
|
增加#literal指令來指定大塊的不需要進(jìn)行渲染的內(nèi)容(類似于\(escaping)的多行版本用法)
|
模板中:
#literal()
#foreach ($woogie in $boogie)
nothing will happen to $woogie
#end
則輸出:
#foreach ($woogie in $boogie)
nothing will happen to $woogie
#end
#end
|
ADD |
增加#evaluate指令在模板渲染時動態(tài)執(zhí)行字符串(類似于JS中的eval()方法)
|
#set($source1 = "abc")
#set($select = "1")
#set($dynamicsource = "$source$select")
## $dynamicsource is now the string '$source1'
#evaluate($dynamicsource)
|
更多請查看
http://velocity.apache.org/engine/devel/changes-report.html
也可以使用
http://velocity.apache.org/engine/devel/user-guide.html
posted on 2008-12-31 17:27
biiau 閱讀(2055)
評論(0) 編輯 收藏