VTL语法大全(Velocity 1.6)

Velocity的出现给程序员减轻了大量的页面工作。Velociy 1.6新增了一些非常有用的指令和功能,并修复了一些bug,用起来比较方便。这里将列出一些新的改变,(其他语法请参考Velocity1.4),希望对大家有所帮助。

TYPE CHANGE eg.
ADD 增加#define指令 定义可以引用的VTL渲染块 #define( $foo )Hello, $bar!#end   #set( $bar = 'world')   $foo 输出: Hello, world!
ADD 增加可以跳出#foreach循环的#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 允许模板中使用数组时当做List来使用,意味着可以使用list的方法 $myarray.isEmpty()            $myarray.size()            $myarray.get(2)            $myarray.set(1, 'test')
ADD 支持模板上调用变参数方法 对于public void setPlanets(String... planets)或者 public void setPlanets(String[] planets)模板上可以这样调用:$sun.setPlanets('Earth', 'Mars', 'Neptune')            $sun.setPlanets('Mercury')            $sun.setPlanets()
ADD 增加严格引用检查设置,必须在#set或者context中设置的变量才能使用,否则会抛出exception(可配置开启关闭) 如果$foo不存在,并且严格引用检查开启            $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指令来指定大块的不需要进行渲染的内容(类似于\(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指令在模板渲染时动态执行字符串(类似于JS中的eval()方法) #set($source1 = "abc")            #set($select = "1")            #set($dynamicsource = "$source$select")            ## $dynamicsource is now the string '$source1'            #evaluate($dynamicsource)


详情请登陆Velocity官方网站
更多请查看 http://velocity.apache.org/engine/devel/changes-report.html
也可以使用 http://velocity.apache.org/engine/devel/user-guide.html

你可能感兴趣的:(apache,html,工作,velocity,sun)