官方文档: http://velocity.apache.org/engine/releases/velocity-1.7/user-guide.html
Notation:
$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, –, _ ][ } ]
Examples:
Notation:
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, –, _ ]* .[a..z, A..Z ][ a..z, A-Z, 0..9, –, _ ]* [ } ]
Examples:
Notation:
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, –, _ ]* .[ a..z, A..Z ][ a..z, A..Z, 0..9, –, _ ]*( [ optional parameter list… ] ) [ } ]
Examples:
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
Format:
# [ { ] set [ } ] ( $ref = [ “, ‘ ]arg[ “, ‘ ] )
Usage:
Examples:
The RHS can also be a simple arithmetic expression, such as:
Format:
# [ { ] if [ } ] ( [condition] ) [output] [ # [ { ] elseif [ } ] ( [condition] ) [output] ]* [ # [ { ] else [ } ] [output] ] # [ { ] end [ } ]
Usage:
Examples (showing different operators):
Operator Name | Symbol | Alternative Symbol | Example |
---|---|---|---|
Equals Number | == | eq | #if( $foo == 42 ) |
Equals String | == | eq | #if( $foo == “bar” ) |
Object Equivalence | == | eq | #if( $foo == $bar ) |
Not Equals | != | ne | #if( $foo != $bar ) |
Greater Than | > | gt | #if( $foo > 42 ) |
Less Than | < | lt | #if( $foo < 42 ) |
Greater Than or Equal To | >= | ge | #if( $foo >= 42 ) |
Less Than or Equal To | <= | le | #if( $foo <= 42 ) |
Boolean NOT | ! | not | #if( !$foo ) |
Notes:
#else
directive.#if( $foo == $bar)it's true!#{else}it's not!#end</li>
Format:
# [ { ] foreach [ } ] ($refinarg)statement# [ { ] end [ } ]
Usage:
Examples of the #foreach(), omitting the statement block :
Velocity provides an easy way to get the loop counter so that you can do something like the following:
<table>
#foreach( $customer in $customerList )
<tr><td>$foreach.count</td><td>$customer.Name</td></tr>
#end
</table>
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:
# The maximum allowed number of loops.
directive.foreach.maxloops = -1
Format:
# [ { ] include [ } ] ( arg[ arg2 … argn] )
Examples:
Format:
# [ { ] parse [ } ] ( arg )
Examples:
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.
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.
Format:
# [ { ] evaluate [ } ] ( arg )
Examples:
Format:
# [ { ] define [ } ] ( $ref )statement# [ { ] end [ } ]
Example:
Format:
# [ { ] macro [ } ] ( vmname arg1 [ arg2 arg3 … argn ] ) [ VM VTL code… ] # [ { ] end [ } ]
Once defined, the VM is used like any other VTL directive in a template.
#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 $!bodyContent as many or few times as you like.
#@vmname( $arg1 $arg2 ) here is the body#end
VMs can be defined in one of two places:
velocity.properties
.Comments are not rendered at runtime.
Example:
## This is a comment.
Example:
#* This is a multiline comment. This is the second line *#
Unparsed content is rendered at runtime, but is not parsed or interpreted.
Example:
#[[ This has invalid syntax that would normally need “poor man’s escaping” like:
]]#