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.
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 [ } ] ( $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:
<table> #foreach( $customer in $customerList ) <tr><td>$velocityCount</td><td>$customer.Name</td></tr> #end </table>
The default name for the loop counter variable reference, which is
specified in the velocity.properties file, is $velocityCount. By
default the counter starts at 1, but this can be set to either 0 or
1 in the velocity.properties
file. Here's what the loop
counter properties section of the velocity.properties
file appears:
# Default name of the loop counter # variable reference. directive.foreach.counter.name = velocityCount # Default starting value of the loop # counter variable reference. directive.foreach.counter.initial.value = 1
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:
# [ { ] 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 )
VMs can be defined in one of two places:
velocity.properties
.