This guide is the reference for the Velocity Template Language (VTL). For more information, please also refer to the Velocity User Guide.
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 $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.
Each method argument can be any valid VTL expression.
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.
Format:
# [ { ] foreach [ } ] ( $ref in arg ) 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:
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:
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 [ = def1 ] [ $arg2 [ = def2 ] $arg3 [ = def3 ] ... $argn [ = defn ] ] ) [ VM VTL code... ] # [ { ] end [ } ]
Once defined, the VM is used like any other VTL directive in a template.
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.
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:
]]#