lovefish
其實不想走,其實想做車! 不想随波逐流 就只能逆流而上!前方有什么,我们无法预见。但是路就在眼前,走还是不走,自己决定。
posts - 11,comments - 10,trackbacks - 0

  Velocity手册

英文源文件:http://jakarta.apache.org/velocity/user-guide.html


1. About this Guide
2. What is Velocity?
3. What can Velocity do for me?
1. The Mud Store example
4. Velocity Template Language (VTL): An Introduction
5. Hello Velocity World!
6. Comments
7. References
1. Variables
2. Properties
3. Methods
8. Formal Reference Notation
9. Quiet Reference Notation
10. Getting literal
1. Currency
2. Escaping Valid VTL References
11. Case Substitution
12. Directives
1. Set
2. String Literals
3. If-Else Statements
1. Relational and Logical Operators
4. Foreach Loops
5. Include
6. Parse
7. Stop
8. Velocimacros
2. Escaping VTL Directives
3. VTL: Formatting Issues
4. Other Features and Miscellany
1. Math
2. Range Operator
3. Advanced Issues: Escaping and !
4. Velocimacro Miscellany
5. String Concatenation
5. Feedback
Velocity
是什
Velocity
是一个基于java的模板引擎(template engine)。它允任何人仅仅简单的使用模板言(template language)来引用由java象。
Velocity用于web开发时,界面设计可以和java程序开发步开发一个遵循MVC架构的web站点,也就是设计可以只面的示效果,而由java程序开发员关业务逻辑编码Velocityjavaweb面中分离出来,这样为web站点的维护提供了便利,同JSPPHP之外又提供了一的方案。
Velocity
的能力不止web站点开发这域,例如,它可以从模板(templateSQLPostScriptXML,它也可以被当作一个独立工具来生源代告,或者作其他系的集成件使用。Velocity也可以Turbine web开发架构提供模板服template service)。Velocity+Turbine提供一个模板服的方式允一个web用以一个真正的MVC模型开发
Velocity
作什
The Mud Store Example                                                  
你是一家专门出售Mud的在线商店的设计们暂且称它"线MUD商店"。你业务很旺,客下了各种类型和数量的mud订单。他都是通过输入用名和密后才登到你的网站,登后就允们查订单购买更多的mud在,一非常流行的mud正在打折售。另外有一些客户规律性的购买另外一也在打折但是不是很流行的Bright Red Mud,由于购买人并不多所以它被安置在面的边缘。所有用的信息都是被跟踪并存放于数据中的,所以某天有一个问题可能会冒出来:不使用velocity来使用更好的浏览趣的商品呢?
Velocity
使得web面的客化工作非常容易。作一个web site设计,你希望个用陆时有自己的面。
你会了一些公司内的件工程,你发现们每个人都同意客户应该拥有具有个性化的信息。那件工程师应该作的事情在一,看一看你应该作些什吧。
你可能在面内嵌套如下的VTL声明:
<html>
<body>
Hello  $customer.Name!
<table>
#foreach( $mud  in  $nudsOnSpecial )
    #if ( $customer.hasPurchased( $mud )  )
        <tr>
            <td>
                $flogger.getPromo( $mud )
            <td>
        <tr>
    #end
#end
</table>
Velocity Template Language(VTL):AN introduction
VTL
意味着提供最简单、最容易并且最整的方式合并动态内容。
VTL
使用references来在web site内嵌套动态内容,一个量就是一种类型的reference量是某种类型的refreence,它可以指向java中的定,或者从当前面内定VTL statement得到。下面是一个VTL statement的例子,它可以被嵌套到HTML中:
 #set ( $a = "Velocity" )
和所有的VTL statementstatement以#字符始并且包含一个directiveset。当一个在线户请求你的Velocity Templating Engine查询整个面以便发现所有#字符,然后确定哪些是VTL statement,哪些不需要VTL作任何事情。
#字符后跟一个directivesetset directive使用一个表达式(使用括号封――一个方程式分配一个值给变量。量被列在左,而它的被列在右,最后他使用=号分割。
在上面的例子中,量是$a,而它的Velocity。和其他的references$字符始,而值总是以双引号封VelocityString可以被赋值给变量。
住以下的规则
使用$字符始的references用于得到什;使用#字符始的directives用于作些什
Hello Velocity World!
一旦某个量被分配了一个,那你就可以在HTML文件的任何地方引用它。在下面的例子中,一个被分配$foo量,并在其后被引用。
 <html>
 <body>
  #set ( $foo = "Velocity" )
  Hello $foo World!
 </body>
 </html>
上面的实现结果是在面上打印"Hello Velocity World"
了使包含VTL directivesstatement更具有可性,我鼓励你在新行VTL statement,尽管你不是必须这么作。Set directive将在后面详细描述。

行注
 ## This is a single line comment.
多行注
 #*
   Thus begins a multi-line comment. Online visitors won't
   see this text because the Velocity Templating Engine will
      ignore it.
 *#
文档格式:
 #**
   This is a VTL comment block and
   may be used to store such information
      as the document author and versioning
   information:
   @version 5
   @author
 *#
References
VTL中有三种类型的references(variables)、属性(properties)、方法(methods)。作一个使用VTL设计者,你和你的工程references的名称达成共,以便你可以在你的template中使用它
Everything coming to and from a reference
被作一个String理。如果有一个$foo是一个Integer象,那Velocity用它的toString()方法将String型。
 
                                                                      
 
格式要求同java
 
属性                                                                      
 
例子:
  $customer.Address
  $purchase.Total
$customer.Address
有两。它可以表示:hashtablecustomer中以Address为关键字的;也可以表示customer象的getAddress()方法。当你的面被Velocity将确定以上两方式用那,然后返回适当的
方法                                                                      
一个方法就是被定java中的一段代,并且它有完成某些有用工作的能力,例如一个算和判断条件是否成立、足等。方法是一个由$始并跟随VTL标识成的References,一般包括一个VTL方法体。例如:
 $customer.getAddress()
 $purchase.getTotal()
 $page.setTitle( "My Home Page" )
 $person.setAttributes( ["Strange", "Weird", "Excited"] )
前两个例子$customer.getAddress()$purchase.getTotal()看起来挺想上面的属性$customer.Address $purchase.Total。如果你得他有某种联系的,那你是正确的。
VTL
属性可以作VTL方法的写。$customer.Address属性和使用$customer.getAddress()方法具有相同的效果。如果可能的使用属性的方式是比合理的。属性和方法的不同点在于你能够给一个方法指定一个参数列表。
正式reference标记
reference
的正是格式如下:
 ${mudSlinger}    

 ${customer.Address}  
属性
 ${purchase.getTotal()}  
方法
非正是格式更常用,但是有时还是使用正是格式比适合。例如:你希望通一个$vice动态组织一个字符串。
 Jack is a $vicemaniac.
本来量是$vice在却成了$vicemaniac这样Veloctiy就不知道您到底要什了。所以,应该使用正是格式
 Jack is a ${vice}maniac
Velocity知道量是$vice而不是$vicemaniac
Quiet reference notation
例如:
 <input  type="text"  name="email"  value="$email" />
面的form被初始加载时$email没有这时你肯定是希望它能够显示一个blank text来代替"$email"这样的字段。那使用quiet reference notation就比合适。
 <input  type="text"  name="email"  value="$!email"/>
这样文本框的初始就不会是email而是空了。
正式和quiet格式的reference notation也可一同使用,像下面这样
 <input  type="text"  name="email"  value="$!{email}"/>
Getting literal                                                             
Velocity
使用特殊字符$#来帮助它工作,所以如果要在template里使用些特殊字符要格外小心。本讨论$字符。
 
货币字符                                                                  
VTL中使用$2.5这样货币标识是没有问题得的,VTL不会将它错认为是一个reference,因VTL中的reference是以一个大写或者小写的字母始。
Escaping valid VTL reference                                             
VTL
中使用"\"逃逸符。
例如:
 #set( $email = "foo" )
 $email
 \$email
 
\\$email
 \\\$email
render
 foo
 $email
 \foo
 
\\$email
如果email量没有被定义则
 $email
 \$email
 
\\$email
 \\\$email
将被render
 $email
 \$email
 
\\$email
 \\\$email
注意:VTL中未被定量将被认为是一个字符串,所以以下例子:
 #set( $foo = "gibbous" )
 $moon = $foo
果是:
$moon = gibbous
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量。
Directives
Reference
许设计者使用动态的内容,而directive使得你可以java来控制你的逻辑,从而达到你所期望的示效果。
 #set                                                                      
 #set directive
被用于置一个reference。例如:
  #set ( $primate = "monkey" )
  #set ( $customer.Behavior = $primate )
赋值的(LHS)必是一个量或者属性reference。右RHS)可以是以下型中一
  
reference
  String literal
  
属性reference
  
方法reference
  number literal
  ArrayList
下面是用各种类型的RHS的例子:
 
set ( $monkey = $bill ) ##reference
 
set ( $monkey.Friend = "monica" ) ##String literal
 
set ( $monkey.Blame = $whitehouse.Leak )##属性reference
 
set ( $monkey.Plan = $spindoctor.weave($web) )##方法reference
 
set ( $monkey.Number = 123 )##Number literal
 
set ( $monkey.Say = ["Not", $my, "fault"] )##ArrayList
注意:最后一个例子的取方法$monkey.Say.get(0)
RHS
也可以是一个简单的算表达式:
 #set ( $value = $foo + 1 )
 #set ( $value = $bar -1 )
#set ( $value = $foo * $bar )
#set ( $value = $foo / $bar )
如果你的RHS是一个nullVTL理将比特殊:它将指向一个已存在的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),它不能被置回nullremoved 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
 String Literals                                                        
 
当你使用#set directiveString literal在一双引号内。
  #set ( $directoryRoot = "www" )
  #set ( $templateName = "index.vm" )
  #set ( $template = "$directoryRoot/$tempateName" )
  $template
 
上面段代www/index.vm
 
但是,当string literal被封装在引号内,它将不被解析:
  #set ( $foo = "bar" )
  $foo
  #set ( $blargh = '$foo' )
 
果:
  bar
  $foo
 
上面个特性可以通修改velocity.properties文件的stringliterals.interpolate = false来改上面的特性是否有效。
条件
 if/elseif/else                                                              
当一个web面被生成使用Velocity#if directrive,如果条件成立的可以在面内嵌入文字。例如:
 #if ( $foo )
  <strong>Velocity!</strong>
 #end
上例中的条件句将在以下两条件下成立:
  $foo
是一个boolean型的量,且它的值为true
  $foo
量的null
里需要注意一点:Velocity context仅仅包含象,所以当我们说"boolean"时实际上代表的一个Boolean象。即便某个方法返回的是一个booleanVelocity也会利用内省机制将它转换为一个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
Velocity
ANDORNOT逻辑运算符。下面是一些例子:
 ## 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
                                                                      
 Foreach
                                                              
 
例子:
  <ul>
   #foreach ( $product in $allProducts )
    <li> $product </li>
   #end
  </ul>
 
次循$allProducts中的一个都会赋给$product量。
$allProducts
可以是一个VectorHashtable或者Array。分配$product是一个java象,并且可以通过变量被引用。例如:如果$product是一个javaProduct,并且品的名字可以通过调用他的getName()方法得到。
在我$allProducts是一个Hashtable,如果你希望得到它的key应该像下面这样
 <ul>
  #foreach ( $key in $allProducts.keySet() )
   <li>Key: $key -> Value: $allProducts.get($key) </li>
  #end
 </ul>
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"始。下面就是文件中的配置:
 # 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将解析其中的VTLrender模板。
 #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 )
 <tr><td></td></tr>
 #end
在上面的例子中Velocimacro被定义为d,然后你就可以在任何VTL directive中以如下方式用它:
 #d()
当你的templateVelocity将用<tr><td></td></tr>换为#d()
Velocimacro可以有任意数量的参数――甚至0个参数,然定义时可以随意置参数数量,但是Velocimacro指定正确的参数。下面是一个有两个参数的Velocimacro,一个参数是color另一个参数是array
 #macro ( tablerows $color $somelist )
 #foreach ( $something in $somelist )
  <tr><td bgcolor=$color>$something</td</tr>
 #end
 #end
#tablerows Velocimacro
 #set ( $greatlakes = [ "Superior", "Michigan", "Huron", "Erie", "Ontario" ] )
 #set ( $color = "blue" )
 <table>
  #tablerows( $color $greatlakes )
 </table>
经过以上的用将生如下的果:
 <table>
  <tr><td bgcolor=" blue"> Superior </td></tr>
  <tr><td bgcolor=" blue"> Michigan </td></tr>
  <tr><td bgcolor=" blue"> Huron </td></tr>
  <tr><td bgcolor=" blue"> Erie </td></tr>
  <tr><td bgcolor=" blue"> Ontario </td></tr>
 </table>
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" )
 <table>
  #tablerows( $cellbgcol $parts )
 </table>
上面VTL生如下的出:
 <table>
  <tr><td bgcolor="#CC00FF"> volva </td</tr>
  <tr><td bgcolor="#CC00FF"> stipe </td</tr>
  <tr><td bgcolor="#CC00FF"> annulus </td</tr>
  <tr><td bgcolor="#CC00FF"> gills </td</tr>
  <tr><td bgcolor="#CC00FF"> pileus </td</tr>
 </table>
 Velocimacro arguments                                                  
 Velocimacro
可以使用以下任何元素作参数:
  Reference
:任何以$开头reference
  String literal

  Number literal

  IntegerRange
[1….3]或者[$foo….$bar]
  
象数["a","b","c"]
  boolean
truefalse
当将一个reference参数传递给Velocimacro注意reference参数是以名字的形式传递的。就意味着参数的Velocimacro才会被生。个特性使得你可以将一个方法用作参数传递给Velocimacro,而Velocimacro都是通过这个方法生不同的行的。例如:
 #macro ( callme $a )
  $a $a $a
 #end
 #callme( $foo.bar() )
行的果是:reference $foobar()方法被行了三次。
如果你不需要这样的特性可以通以下方法:
 #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
属性有两个可选值truefalse个属性允使用者确定inlineVelocimacro是否可以替代全局Velocimacro(比如在velocimacro.library属性中指定的文件内定Velocimacro)。默情况下,此值为false这样就阻止本地Velocimacro覆盖全局定
Velocity.properties
文件中的
velocimacro.permissions.allow.inline.local.scale
属性也是有truefalse两个可选值,默false。它的作用是用于确定你inlineVelocimacros是否仅仅在被定template内可话说,如果个属性true,一个inlineVelocimacros只能在定它的template内使用。你可以使用此实现一个奇妙的VMa 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属性有truefalse两个可选值,默认值为false。当true,任何在Velocimacro内通#set()context的修改被认为针对velocimacro的本地置,而不会永久的影响内容。
Velocity.properties
文件中的velocimacro.library.autoreload属性控制Velocimacro的自。默false。当ture于一个Velocimacro用将自动检查原始是否生了化,如果化将重新加它。个属性使得你可以不用重新启servlet容器而达到重新加的效果,就像你使用regular模板一个属性可以使用的前提就是resource loader存是offfile.resource.loader.cache = false)。注意个属性实际上是针对开发而非品的。
Velocimacro Trivia                                                        
Velocimacro
被定在他被使用之前。也就是,你的#macro()声明应该在使用Velocimacros之前。
要注意的是,如果你试图#parse()一个包含#macro()的模板。因#parse()生在运行期,但是解析器在parsetiem决定一个看似VM元素的元素是否是一个VM元素,这样#parse()-ingVM声明将不按照期的子工作。了得到期的果,只需要你简单的使用velocimacro.library使得Velocity在启动时你的VMs
Escaping VTL directives
VTL directives can be escaped with "\"
号,使用方式跟VTLreference使用逃逸符的格式差不多。
 ## #include( "a.txt" ) renders as <ontents of a.txt>(
)
 #include( "a.txt" )

 ## \#include( "a.txt" ) renders as \#include( "a.txt" )
 \#include( "a.txt" )

 ## \\#include ( "a.txt" ) renders as \<contents of a.txt>
 
\\#include( "a.txt" )
在一个directive内包含多个script元素的VTL directives使用逃逸符要特小心(比如在一个if-else-end statement内)。下面是VTLif-statement的典型用:
 #if ( $jazz )
  Vyacheslav Ganelin
 #end
如果$jazzture出将是:
 Vyacheslav Ganelin
如果$jazzfalse,将没有出。使用逃逸符将改变输出。考一下下面的情况:
 \#if ( $jazz )
  Vyacheslav Ganelin
 \#end
在无$jazztruefalse果都是:
 #if ( $jazz )
  Vyacheslav Ganelin
 #end
上,由于你使用了逃逸符,$jazz根本就没有被解析boolean。在逃逸符前使用逃逸符是合法的,例如:
 
\\#if ( $jazz )
  Vyacheslav Ganelin
 
\\#end
以上程序的
 \ Vyacheslav Ganelin
 \
但是如果$jazzfalse,那将没有出。(会没有出,但是我应该还有有"\"字符被出。)
VTL
Formatting issues
尽管在此用手册中VTL通常都始一个新行,如下所示:
 #set ( $imperial = [ "Munetaka", "Koreyasu", "Hisakira", "Morikune" ] )
 #foreach ( $shogun in $imperial )
  $shogun
 #end
但是像下面这种写法也是可以的:
 Send me #set($foo = ["$10 and","a cake"])#foreach($a in $foo)$a #end please.
上面的代可以被改写
 Send me
 #set ( $foo = ["$10 and ","a cake"] )
 #foreach ( $a in $foo )
  $a
 #end
 please.
或者
 Send me
 #set($foo      = ["$10 and ","a cake"])
    #foreach        
$a in $foo $a
   #end please.
构将一
其他特性和杂项
 math                                                                       
在模板中可以使用Velocity内建的算函数,如:加、减、乘、除
  #set ( $foo = $bar + 3 )
  #set ( $foo = $bar - 4 )
  #set ( $foo = $bar * 6 )
  #set ( $foo = $bar / 2 )
 
行除法将返回一个Integer型的果。而余数你可以使用%来得到:
  #set ( $foo = $bar % 5 )
Velocity内使用数学算公式,只能使用像-n,-2,-1,0,1,2,n这样的整数,而不能使用其它型数据。当一个非整型的象被使用它将被logged并且将以null为输果。
Range Operator
Range operator
可以被用于与#set#foreach statement合使用。理一个整型数它是很有用的,Range operator具有以下构造形式:
 [n..m]
m
n都必是整型,而m是否大于n关紧要。例子:
 First example:
 #foreach ( $foo in [1..5] )
  $foo
 #end

 Second example:
 #foreach ( $bar in [2..-2] )
  $bar
 #end

 Third example:
 #set ( $arr = [0..1] )
 #foreach ( $i in $arr )
  $i
 #end

 Fourth example:
 [1..3]
上面四个例子的
 First example

 1 2 3 4 5

 Second example
 2 1 0 -1 -2

 Third example
 0 1

 Fourth example
 [1..3]
注意:range operator只在#set#foreach中有效。
Advanced Issue
Escaping and
当一个reference""分隔,并且在它之前有逃逸符reference将以特殊的方式理。注意这种方式与准的逃逸方式不同的。照如下:
#set ( $foo = "bar" )
特殊形式 准格式
Render
 Render Render Render
$\!foo $!foo \$foo \$foo
$\!{foo} $!{foo} \$!foo \$!foo
$\\!foo $\!foo \$!{foo} \$!{foo}
$\\\!foo $\\!foo 
\\$!{foo} \bar
Velocimacro
杂记
 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' ) )
注意在下面的例子中参数被evaluatedVelocimacro内部,而不是在calling level。例子:
 #macro ( inner $foo )
  inner : $foo
 #end

 #macro ( outer $foo )
  #set ( $bar = "outerlala" )
  outer : $foo
 #end
 
 #set ( $bar = 'calltimelala' )
 #outer( "#inner($bar)" )

 outer : inner : outerlala
Veloctiy的特性:参数的传递By Name的。例如:
 #macro ( foo $color )
  <tr bgcolor = $color ><td>Hi</td></tr>
  <tr bgcolor = $color ><td>There</td></tr>
 #end

 #foo ( $bar.rowColor() )
以上代rowColor()方法两次用,而不是一次。了避免这种现象的出,我可以按照下面的方式行:
 #set ( $color = $bar.rowColor() )
 #foo ( $color )
can I register velocimacros via #parse()?                                
目前,Velocimacros在第一次被模板用前被定就意味着你的#macro()声明应该在使用Velocimacros之前。
如果你试图#parse()一个包含#macro() directive的模板,一点是需要牢的。因#parse()生在运行期,但是解析器在parsetiem决定一个看似VM元素的元素是否是一个VM元素,这样#parse()-ingVM声明将不按照期的子工作。了得到期的果,只需要你简单的使用velocimacro.library使得Velocity在启动时你的VMs
What is velocimacro autoreloading
                                      
velocimacro.library.autoreload
专门为开发而非品使用的一个属性。此属性的默认值false
String concatenation
开发最常问题是我如何作字符拼接?在java中是使用""号来完成的。
VTL里要想实现的功能你只需要将需要合的reference放到一起就行了。例如:
#set ( $size = "Big" )
#set ( $name = "Ben" )
The clock is $size$name.
果将是:The clock is BigBen.。更有趣的情况是:
 #set ( $size = "Big" )
 #set ( $name = "Ben" )
 #set ( $clokc = "$size$name" )
 The clock is $clock.
上例也会得到同果。最后一个例子,当你希望混合固定字段到你的reference,你需要使用准格式:
 #set ( $size = "Big" )
 #set ( $name = "Ben" )
 #set ( $clock = "${size}Tall$name" )
 The clock is $clock.
果是:The clock is BigTallBen.。使用这种格式主要是了使得$size不被解释为$sizeTall

 

posted on 2007-05-21 13:27 砸你家玻璃 阅读(1289) 评论(1)  编辑  收藏 所属分类: Velocity

FeedBack:
# re: Velocity用户手册.. 英文源文件:http://jakarta.apache.org/velocity/user-guide.html
2007-11-14 16:00 | jangill
谢谢共享  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: