If / ElseIf / Else
The #if
directive in Velocity allows for text to be included when
the web page is generated, on the conditional that the if statement is true. For
example:
#if( $foo )
<strong>Velocity!</strong>
#end
The variable $foo
is evaluated to determine whether it is true,
which will happen under one of two circumstances: (i) $foo
is a
boolean (true/false) which has a true value, or (ii) the value is not null. Remember
that the Velocity context only contains Objects, so when we say "boolean," it will
be represented as a Boolean (the class). This is true even for methods that return
boolean - the introspection infrastructure will return a Boolean of the same logical
value.
The content between the #if
and the #end
statements
becomes the output if the evaluation is true. In this case, if $foo
is true, the output will be: "Velocity!" Conversely, if $foo
has a
null value, or if it is a boolean false, the statement evaluates as false, and there
is no output.
An #elseif
or #else
element can be used with an
#if
element. Note that the Velocity Templating Engine will stop
at the first expression that is found to be true. In the following example, suppose
that $foo
has a value of 15 and $bar
has a value
of 6.
#if( $foo < 10 )
<strong>Go North</strong>
#elseif( $foo == 10 )
<strong>Go East</strong>
#elseif( $bar == 6 )
<strong>Go South</strong>
#else
<strong>Go West</strong>
#end
In this example, $foo
is greater than 10, so the first two
comparisons fail. Next $bar
is compared to 6, which is true, so the
output is Go South.
Relational and Logical Operators
Velocity uses the equivalent operator to determine the relationships between variables. 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
Note that the semantics of ==
are slightly different than Java where
==
can only be used to test object equality. In Velocity the
equivalent operator can be used to directly compare numbers, strings, or objects. When the objects are of different classes, the string representations are obtained
by calling toString
() for each object, then compared.
Velocity has logical AND, OR and NOT operators as well. Below are examples demonstrating the use of the logical AND, OR and NOT operators.
## logical AND
#if( $foo && $bar )
<strong> This AND that</strong>
#end
The #if()
directive will evaluate to true only if both
$foo
and $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
;
if $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.
Logical OR operators work the same way, except only one of the references needs to evaluate to true in order for the entire expression to be considered true. Consider the following example.
## logical OR
#if( $foo || $bar )
<strong>This OR That</strong>
#end
If $foo
is true, the Velocity Templating Engine has no need to look
at $bar
; whether $bar
is true or false, the
expression will be true, and This OR That will be output. If
$foo
is false, however, $bar
must be checked. In this case, if $bar
is also false, the expression evaluates to
false and there is no output. On the other hand, if $bar
is true,
then the entire expression is true, and the output is This OR That
With logical NOT operators, there is only one argument:
##logical NOT
#if( !$foo )
<strong>NOT that</strong>
#end
Here, the if $foo
is true, then !$foo
evaluates to
false, and there is no output. If $foo
is false, then
!$foo
evaluates to true and NOT that will be output. Be
careful not to confuse this with the quiet reference $!foo
which is
something altogether different.
There are text versions of all logical operators, including eq
,
ne
, and
, or
,
not
, gt
, ge
,
lt
, and le
.
One more useful note: when you want to include text immediately following a
#else
directive you will need to use curly brackets immediately
surrounding the directive to differentiate it from the following text. (Any
directive can be delimited by curly brackets, although this is most useful for
#else
).
#if( $foo == $bar)it's true!#{else}it's not!#end</li>