Velocity用户手册 - 中文版(学习修改版) 联系客服

发布时间 : 星期一 文章Velocity用户手册 - 中文版(学习修改版)更新完毕开始阅读3122e841178884868762caaedd3383c4bb4cb425

#foreach ( $customer in $customerList )

$velocityCount$customer.Name #end

$velocityCount变量的名字是Velocity默认的名字,你也可以通过修改

velocity.properties文件来改变它。默认情况下,计数从“1”开始,但是你可以在velocity.properties设置它是从“1”还是从“0”开始。下面就是文件中的配置:

# Default name of loop counter # variable reference

directive.foreach.counter.name = velocityCount

# Default starting value of the loop # counter variable reference

directive.foreach.counter.initial.value = 1

include

#include script element允许模板设计者引入本地文件。被引入文件的内容将不会通过模板引擎被render。为了安全的原因,被引入的本地文件只能在TEMPLATE_ROOT目录下。

#inclued ( “one.txt” )

如果您需要引入多个文件,可以用逗号分隔就行:

#include ( “one.gif”, “two.txt”, “three.htm” ) 在括号内可以是文件名,但是更多的时候是使用变量的: #inclue ( “greetings.txt”, $seasonalstock )

parse

#parse script element允许模板设计者一个包含VTL的本地文件。Velocity将解析其中的VTL并render模板。 #parse( “me.vm” )

就像#include,#parse接受一个变量而不是一个模板。任何由#parse指向的模板都必须包含在TEMPLATE_ROOT目录下。与#include不同的是,#parse只能指定单个对象。

你可以通过修改velocity.properties文件的parse_direcive.maxdepth的值来控制一个template可以包含的最多#parse的个数――默认值是10。#parse是可以递归调用的,例如:如果dofoo.vm包含如下行: Count down.

#set ( $count = 8 )

#parse ( “parsefoo.vm” ) All done with dofoo.vm!

那么在parsefoo.vm模板中,你可以包含如下VTL: $count

#set ( $count = $count – 1 ) #if ( $count > 0 )

#parse( “parsefoo.vm” ) #else

All done with parsefoo.vm! #end

的显示结果为: Count down. 8 7 6 5 4 3 2 1 0

All done with parsefoo.vm! All done with dofoo.vm!

Stop

#stop script element允许模板设计者停止执行模板引擎并返回。把它应用于debug是很有帮助的。 #stop

Velocimacros #macro script element允许模板设计者定义一段可重用的VTL template。例如: #macro ( d )

#end

在上面的例子中Velocimacro被定义为d,然后你就可以在任何VTL directive中以如下方式调用它: #d()

当你的template被调用时,Velocity将用替换为#d()。 每个Velocimacro可以拥有任意数量的参数――甚至0个参数,虽然定义时可以随意设置参数数量,但是调用这个Velocimacro时必须指定正确的参数。下面是一个拥有两个参数的Velocimacro,一个参数是color另一个参数是array: #macro ( tablerows $color $somelist ) #foreach ( $something in $somelist )

$something #end #end

调用#tablerows Velocimacro: #set ( $greatlakes = [ “Superior”, “Michigan”, “Huron”, “Erie”, “Ontario” ] )

#set ( $color = “blue” )

#tablerows( $color $greatlakes )

经过以上的调用将产生如下的显示结果:

Superior
Michigan
Huron
Erie
Ontario

Velocimacros可以在Velocity模板内实现行内定义(inline),也就意味着同一个web site内的其他Velocity模板不可以获得Velocimacros的定义。定义一个可以被所有模板共享的Velocimacro显然是有很多好处的:它减少了在一大堆模板中重复定义的数量、节省了工作时间、减少了出错的几率、保证了单点修改。

上面定义的#tablerows( $color $list )Velocimacro被定义在一个

Velocimacros模板库(在velocity.properties中定义)里,所以这个macro可以在任何规范的模板中被调用。它可以被多次应用并且可以应用于不同的目的。例如下面的调用:

#set ( $parts = [ “volva”, “stipe”, “annulus”, “gills”, “pileus” ] )

#set ( $cellbgcol = “#CC00FF” )

#tablerows( $cellbgcol $parts )

上面VTL将产生如下的输出:

volva
stipe
annulus
gills
pileus

Velocimacro arguments

Velocimacro可以使用以下任何元素作为参数: l Reference:任何以$开头的reference l String literal: l Number literal:

l IntegerRange:[1?.3]或者[$foo?.$bar] l 对象数组:[“a”,”b”,”c”] l boolean值:true、false

当将一个reference作为参数传递给Velocimacro时,请注意reference作为参数时是以名字的形式传递的。这就意味着参数的值在每次Velocimacro内执行时

才会被产生。这个特性使得你可以将一个方法调用作为参数传递给Velocimacro,而每次Velocimacro执行时都是通过这个方法调用产生不同的值来执行的。例如: #macro ( callme $a ) $a $a $a #end

#callme( $foo.bar() )

执行的结果是:reference $foo的bar()方法被执行了三次。 如果你不需要这样的特性可以通过以下方法: #set ( $myval = $foo.bar() ) #callme ( $myval )

Velocimacro properties

Velocity.properties文件中的某几行能够使Velocimacros的实现更加灵活。注意更多的内容可以看Developer Guide。

Velocity.properties文件中的velocimacro.libraary:一个以逗号分隔的模板库列表。默认情况下,velocity查找唯一的一个库:VM_global_library.vm。你可以通过配置这个属性来指定自己的模板库。

Velocity.properties文件中的velocimacro.permissions.allow.inline属性:有两个可选的值true或者false,通过它可以确定Velocimacros是否可以被定义在regular template内。默认值是ture――允许设计者在他们自己的模板中定义Velocimacros。

Velocity.properties文件中的

velocimacro.permissions.allow.inline.replace.global属性有两个可选值true和false,这个属性允许使用者确定inline的Velocimacro定义是否可以替代全局Velocimacro定义(比如在velocimacro.library属性中指定的文件内定义的Velocimacro)。默认情况下,此值为false。这样就阻止本地Velocimacro定义覆盖全局定义。

Velocity.properties文件中的

velocimacro.permissions.allow.inline.local.scale属性也是有true和false两个可选值,默认是false。它的作用是用于确定你inline定义的

Velocimacros是否仅仅在被定义的template内可见。换句话说,如果这个属性设置为true,一个inline定义的Velocimacros只能在定义它的template内使用。你可以使用此设置实现一个奇妙的VM敲门:a template can define a private implementation of the second VM that will be called by the first VM when invoked by that template. All other templates are unaffected。

Velocity.properties文件中的velocimacro.context.localscope属性有true和false两个可选值,默认值为false。当设置为true时,任何在Velocimacro内通过#set()对context的修改被认为是针对此velocimacro的本地设置,而不会永久的影响内容。

Velocity.properties文件中的velocimacro.library.autoreload属性控制Velocimacro库的自动加载。默认是false。当设置为ture时,对于一个

Velocimacro的调用将自动检查原始库是否发生了变化,如果变化将重新加载它。这个属性使得你可以不用重新启动servlet容器而达到重新加载的效果,就像你使用regular模板一样。这个属性可以使用的前提就是resource loader缓存是