Velocity knows a number of relational and logical operators. They
can be used everywhere an expression is evaluated, most prominently in
Each operator is available in two notations, a short version which
is roughly equal to the Java notation and a text representation which can
be used to avoid problems e.g. with XML templates and the
Example 6.1. Using operators
#set ($a = true) #set ($b = false) #if ($a || $b) short version 'or' was true #end #if ($a and $b) text version 'and' was true #end
Table 6.1. Velocity Relational and logical operators
|Type of operator||short version||text version|
|greater or equal than||>=||ge|
|less or equal than||<=||le|
 Note that the semantics of the equal operator are
slightly different than Java where
Unlike other languages, Velocity does not consider the number 0
(zero) or the empty String ('') to be equivalent to false. Only the
boolean value false (primitive oder
Here is a simple example to illustrate how the equivalent operator is used.
#set ($foo = "deoxyribonucleic acid") #set ($bar = "ribonucleic acid") #if ($foo == $bar) In this case it's clear they aren't equivalent. So... #else They are not equivalent and this will be the output. #end
The text versions of all logical operators can be used to avoid problems with < and > in XML templates.
## logical AND #if( $foo && $bar ) <strong>This AND that</strong> #end
#if() directive will only evaluate to true
$bar are true. If
$foo is false, the expression will evaluate to false;
$bar will not be evaluated. If
$foo is true, the Velocity Templating Engine will
then check the value of
$bar is true, then the entire expression is true and
This AND that becomes the output. If
$bar is false,
then there will be no output as the entire expression is false.
 This was a conscious decision by the Velocity designers and is not likely to change.