二、注释 1.单行注释: ## This is a single line comment. 2.多行注释: #* Thus begins a multi-line comment. Online visitors won’t see this text because the Velocity Templating Engine will ignore it. *# 3.文档格式: #** This is a VTL comment block and may be used to store such information as the document author and versioning information: @version 5 @author *#
作为一个使用VTL的页面设计者,你和你的工程师必须就references的名称达成共识,以便你可以在你的template中使用它们。 Everything coming to and from a reference被作为一个String对象处理。如果有一个对象$foo是一个Integer对象,那么Velocity将调用它的toString()方法将这个对象转型为String类型。
4.正式reference标记 reference的正是格式如下: ${mudSlinger} 变量 ${customer.Address} 属性 ${purchase.getTotal()} 方法 非正是格式更见常用,但是有时还是使用正是格式比较适合。例如:你希望通过一个变量$vice来动态的组织一个字符串。 Jack is a $vicemaniac. 本来变量是$vice现在却变成了$vicemaniac,这样Veloctiy就不知道您到底要什么了。所以,应该使用正是格式书写 Jack is a ${vice}maniac 现在Velocity知道变量是$vice而不是$vicemaniac。
7.Case substitution 现在你已经对reference比较熟悉了,你可以将他们高效的应用于你的template了。Velocity利用了很多java规范以方便了设计人员的使用。例如: $foo $foo.getBar() ## is the same as $foo.Bar
$data.getUser(“jon”) ## is the same as $data.User(“jon”)
$data.getRequest().getServerName() # is the same as $data.Request.ServerName ## is the same as ${data.Request.ServerName} 但是,注意VTL中不会将reference解释为对象的实例变量。例如:$foo.Name将被解释为Foo对象的getName()方法,而不是Foo对象的Name实例变量。
如果你的RHS是一个null,VTL的处理将比较特殊:它将指向一个已经存在的reference,这对初学者来讲可能是比较费解的。例如: #set ( $resut = $query.criteria(“name”) ) The result of the first query is $result
#set ( $resut = $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 first query is bill 看看下面的例子: #set( $criteria = ["name", "address"] ) #foreach( $criterion in $criteria ) #set( $result = $query.criteria($criterion) ) #if( $result ) Query was successful #end #end 在上面的例子中,程序将不能智能的根据$result的值决定查询是否成功。在$result被#set后(added to the context),它不能被设置回null(removed from the context)。打印的结果将显示两次查询结果都成功了,但是实际上有一个查询是失败的。 为了解决以上问题我们可以通过预先定义的方式: #set( $criteria = [“name”, “address”] ) #foreach( $criterion in $criteria ) #set( $result = false ) #set( $result = $query.criteria( $criterion ) ) #if( $result ) Query was successful #end #end
当一个web页面被生成时使用Velocity的#if directrive,如果条件成立的话可以在页面内嵌入文字。例如: #if ( $foo ) <strong>Velocity!</strong> #end 上例中的条件语句将在以下两种条件下成立: l $foo是一个boolean型的变量,且它的值为true l $foo变量的值不为null 这里需要注意一点:Velocity context仅仅能够包含对象,所以当我们说“boolean”时实际上代表的时一个Boolean对象。即便某个方法返回的是一个boolean值,Velocity也会利用内省机制将它转换为一个Boolean的相同值。 如果条件成立,那么#if和#end之间的内容将被显示。 #elseif和#else元素可以同#if一同使用。例如: #if( $foo < 10 ) <strong> Go North </strong> #elseif( $foo == 10 ) <strong> Go East </strong> #elseif( $foo == 6 ) <strong> Go South </strong> #else <strong> Go West </strong> #end 注意这里的Velocity的数字是作为Integer来比较的――其他类型的对象将使得条件为false,但是与java不同它使用“==”来比较两个值,而且velocity要求等号两边的值类型相同。 关系、逻辑运算符 Velocity中使用等号操作符判断两个变量的关系。例如: #set ( $foo = “deoxyribonucleic acid” ) #set ( $bar = “ribonucleic acid” ) #if ( $foo == $foo ) In this case it’s clear they aren’t equivalent.So… #else They are not equivalent and this will be the output. #end
3.Velocity有AND、OR和NOT逻辑运算符。
下面是一些例子: ## logical AND #if( $foo && $bar ) <strong> This AND that </strong> #end
## logical OR #if ( $foo $bar ) <strong>This OR That </strong> #end
##logical NOT #if ( !$foo ) <strong> NOT that </strong> #end 4.循环 :Foreach循环
9.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
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缓存是off状态(file.resource.loader.cache = false)。注意这个属性实际上是针对开发而非产品的。 Velocimacro Trivia Velocimacro必须被定义在他们被使用之前。也就是说,你的#macro()声明应该出现在使用Velocimacros之前。 特别要注意的是,如果你试图#parse()一个包含#macro()的模板。因为#parse()发生在运行期,但是解析器在parsetiem决定一个看似VM元素的元素是否是一个VM元素,这样#parse()-ing一组VM声明将不按照预期的样子工作。为了得到预期的结果,只需要你简单的使用velocimacro.library使得Velocity在启动时加载你的VMs。
11.Escaping VTL directives
VTL directives can be escaped with “/”号,使用方式跟VTL的reference使用逃逸符的格式差不多。 ## #include( “a.txt” ) renders as <ontents of a.txt>(注释行) #include( “a.txt” )
Can I user a directive or another VM as an argument to a VM? 例如:#center ( #bold( “hello” ) ) 不可以。一个directive的参数使用另外一个directive是不合法的。 但是,还是有些事情你可以作的。最简单的方式就是使用双引号: #set ( $stuff = “#bold( ‘hello’ )” ) #center ( $stuff ) 上面的格式也可以缩写为一行: #center ( “#bold( ‘hello’ ) ) 请注意在下面的例子中参数被evaluated在Velocimacro内部,而不是在calling level。例子: #macro ( inner $foo ) inner : $foo #end