翻译自: 官网 Apache Velocity Project
$ [ ! ][ { ][a…z, A…Z ][ a…z, A…Z, 0…9, -, _ ][ } ]
示例:
$mud-Slinger_9
$!mud-Slinger_9
${mud-Slinger_9}
$!{mud-Slinger_9}
Notation:
$ [ { ][ a…z, A…Z ][ a…z, A…Z, 0…9, -, _ ] .[a…z, A…Z ][a…z, A-Z, 0…9, -, _ ] [ } ]
示例:
$customer.Address
${purchase.Total}
对于 getProp(), isProp 可以用 $obj.prop 代替
Notation:
$ [ { ][ a…z, A…Z ][ a…z, A…Z, 0…9, -, _ ] .[ a…z, A…Z ][a…z, A…Z, 0…9, -, _ ]( [optional parameter list…* ] ) [ } *]
示例:
$customer.getAddress()
${purchase.getTotal()}
$page.setTitle( “My Home Page” )
VTL Properties can be used as a shorthand notation for VTL Methods that take get and set. Either $object.getMethod() or $object.setMethod() can be abbreviated as $object.Method. It is generally preferable to use a Property when available. The main difference between Properties and Methods is that you can specify a parameter list to a Method.
对于 $object.getMethod(), $object.getMethod() 方法 可以使用 $object.method 代替
格式: # [ { ] set [ } ] ( $ref = [ ", ’ ]arg[ ", ’ ] )
参数说明:
$ref - The LHS of the assignment must be a variable reference or a property reference.
表达式左侧必须是一个变量引用或者属性引用
arg - The RHS of the assignment, arg is parsed if enclosed in double quotes, and not parsed if enclosed in single quotes. If the RHS evaluates to null, it is not assigned to the LHS.
表达式右侧,如果由双引号包裹,会被解析,单引号则不会,如果右侧表达式值为null,则不会被赋值
双引号单独说明一下
$className 为 “test0”
#{set}($test=“$className”)
$test // 输出为 test0
Examples:
Variable reference: #set( $monkey = $bill )
变量引用
String literal: #set( $monkey.Friend = ‘monica’ )
字符串字面量
Property reference: #set( $monkey.Blame = $whitehouse.Leak )
属性用用
Method reference: #set( $monkey.Plan = s p i n d o c t o r . w e a v e ( spindoctor.weave( spindoctor.weave(web) )
方法引用
Number literal: #set( $monkey.Number = 123 )
数字字面量
Range operator: #set( $monkey.Numbers = [1…3] )
数组
Object list: #set( $monkey.Say = [“Not”, $my, “fault”] )
对象列表
Object map: #set( $monkey.Map = {“banana” : “good”, “roast beef” : “bad”})
对象map
The RHS can also be a simple arithmetic expression, such as:
右侧表达式也可以是简单的数学表达式
加
Addition: #set( $value = $foo + 1 )
减
Subtraction: #set( $value = $bar - 1 )
乘
Multiplication: #set( $value = $foo * $bar )
除
Division: #set( $value = $foo / $bar )
取余
Remainder: #set( $value = $foo % $bar )
Format:
# [ { ] if [ } ] ( [condition] ) [output] [# [ { ] elseif [ } ] ( [condition] ) [output] ]* [# [ { ] else [ } ] [output] ] # [ { ] end [ } ]
Usage:
condition - If a boolean, considered true if it has a true false; if not a boolean, considered true if not null.
output - May contain VTL.
The == operator can be used to compare numbers, strings, objects of the same class, or objects of different classes. In the last case (when objects are of different classes), the toString() method is called on each object and the resulting Strings are compared.
== 操作符可以用来比较 数字,字符串,相同类型对象,不同类型对象。如果是不同类型对象,会先调用 toString 方法,然后使用字符串比较
# [ { ] foreach [ } ] ($ref in arg) statement # [ { ] end [ } ]
Examples of the #foreach(), omitting the statement block :
Reference: #foreach ( $item in $items )
Array list: #foreach ( $item in [“Not”, $my, “fault”] )
Range operator: #foreach ( $item in [1…3] )
Additionally, the maximum allowed number of loop iterations can be controlled engine-wide (an ability introduced in Velocity 1.5). By default, there is no limit:
可以通过设置,指定 foreach list的最大长度
# The maximum allowed number of loops.
directive.foreach.maxloops = -1
Format:
# [ { ] include [ } ] ( arg[ arg2 … argn] )
Usage:
arg - Refers to a valid file under TEMPLATE_ROOT.
路径一定要是 模板根目录下的
Examples:
String: #include( “disclaimer.txt” “opinion.txt” )
字符串引用
Variable: #include( $foo $bar )
变量引用
Format:
# [ { ] parse [ } ] ( arg )
Usage:
arg - Refers to a template under TEMPLATE_ROOT.
Examples:
String: #parse( “lecorbusier.vm” )
Variable: #parse( $foo )
允许循环引用,默认最深10层。可以设置参数 parse_directive.maxdepth 决定最大深度
Recursion permitted. See parse_directive.maxdepth in velocity.properties to change from parse depth. (The default parse depth is 10.)
Format:
# [ { ] stop [ } ]
Usage:
This will stop execution of the current template. This is good for debugging a template.
停止解析模板,可以用来debug模板解析
Format:
# [ { ] break [ } ]
Usage:
This will break execution of the current content directive. This is good for exiting a #foreach loop early, but also works in other scopes. You can even pass the scope control reference for a specific outer scope to break execution of all scopes outward to the specified one.
停止当前指令解析,也可以跳出当前 scope, 到外部 scope
Format:
# [ { ] evaluate [ } ] ( arg )
Usage:
arg - String literal or reference to be dynamically evaluated.
字符串或者字符串引用
Examples:
String: #evaluate( ‘string with VTL #if(true)will be displayed#end’ )
Variable: #evaluate( $foo )
Format:
# [ { ] define [ } ] ( $ref )statement# [ { ] end [ } ]
Usage:
$ref - Reference that is assigned the VTL block as a value.
标识 “块” 的名称
statement - Statement that is assigned to the reference.
“块” 的内容
Example:
#define( $hello ) Hello $who #end
#set( $who = "World!")
$hello ## displays Hello World!
Format:
# [ { ] macro [ } ] ( vmname $arg1 [ $arg2 $arg3 … $argn ] ) [ VM VTL code… ] # [ { ] end [ } ]
Usage:
#vmname( $arg1 $arg2 )
Except, that when you wish to call a VM with a body, then you must prefix the name of the VM with @. The content of that body may be referenced in the macro definition via ! b o d y C o n t e n t a s m a n y o r f e w t i m e s a s y o u l i k e . 特 别 的 , 当 你 想 使 用 带 b o d y 的 参 数 调 用 时 , 必 须 要 在 块 名 称 前 加 上 “ @ ” 。 b o d y 内 容 可 以 在 块 中 使 用 , 通 过 ‘ !bodyContent as many or few times as you like. 特别的,当你想使用 带 body的参数调用时,必须要在块名称前加上“@”。body 内容可以在块中使用,通过 ` !bodyContentasmanyorfewtimesasyoulike.特别的,当你想使用带body的参数调用时,必须要在块名称前加上“@”。body内容可以在块中使用,通过‘!bodyContent` 使用
#@vmname( $arg1 $arg2 ) here is the body#end
VMs can be defined in one of two places:
带参数块可以定义在两个地方
## This is a comment.**
#**
This is a multiline comment.
This is the second line.
*#
#[[
This has invalid syntax that would normally need
"poor man's escaping" like:
- #define()
- ${blah
]]#