Date: Fri, 29 Mar 2024 16:40:25 +0100 (CET) Message-ID: <1492954.2250.1711726825581@nm-docs> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_2249_2099049291.1711726825581" ------=_Part_2249_2099049291.1711726825581 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
The #if directive allows you to include text wh= en generating a document, on condition that the if-statement is true, for e= xample:
#if ($c= ondition)=20 Hello World=20 #end
The variable: $condition is evaluated to determi=
ne whether it is true, which will happen under certain circumstances:
If $condition returns 'true', the content between the <= strong>#if and #end statements = becomes the output. In this case, if $condition i= s true, the output will be: "Hello World". Conversely, if $cond= ition returns 'false', there will be no output.
The #elseif or #else= element can be used with the #if element. Note t= hat Velocity Engine will stop at the first expression that is found to be t= rue. The following example shows you how to add #elseif an= d #else to the #if statement:
#if( $c= ondition )=20 Content 1=20 #elseif( $condition2 )=20 Content 2=20 #elseif( $condition3 )=20 Content 3=20 #else=20 Content 4=20 #end
From the above example, let us assume that $condition1 is false, $condition2 is true, and $condition3 is true. The output for this conditional blo=
ck will be Content 2 because $conditi=
on2 comes before $condition3 even t=
hough both of them are true.
Compa=
ring Values and Logical Operators
So far, $condition in the #if directive is assumed to be a Bool= ean value, however just like Java, Velocity supports the syntax to compare = (greater than (>), less than (<), and is equal to (=3D=3D)) two varia= bles and the Logical Operators logical AND (&&), logical OR (||), a= nd logical NOT (!) which will return a Boolean value.
In Velocity, the equivalent operator ("=3D=3D") can be used to compare s= tring, value, and objects. Note that the semantics of the equivalent operat= or ("=3D=3D") are slightly different than those of Java where the equivalen= t operator ("=3D=3D") can only be used to test object equality. In Velocity= the equivalent operator ("=3D=3D") can be used to directly compare numbers= , strings, or objects. When comparing two objects with different classes, t= he string that represents the objects is compared. The following example co= mpares two variables to see whether or not they are equal:
#set ($= var1 =3D "cat")=20 #set($var2 =3D "dog")=20 #if ($var1 =3D=3D $var2)=20 Var1 equals Var2.=20 #else=20 Var1 does not equal Var2=20 #end
The comparison operators ("<" and ">") is the same as the ones used =
in Java. The following example shows how to use the comparison operators to=
compare two values in which the #if statement will be eva=
luated 'true' and "Var1 is less than Var2" will be printed=
out in the generated report:
#set ( = $var1 =3D 6 )=20 #set ( $var2 =3D 7 )=20 #if ( $var1 < $var2 )=20 Var 1 is less than Var2=20 #end
The logical AND, in Velocity, is represented by && and it must hav=
e at least two arguments. To write a conditional statement with the logical=
AND type, for example:
#if( $v= ar1 < $var3 && $var3 > $var2 )=20 Content 1=20 #end
To better understand the above example, let us assume that $var1=
< $var3 is argument 1 and $var3 > =
$var2 is argument 2 (argument 1 =
and argument 2 will be evaluated separately and will retur=
n true/false). The #if() directive will be evalua=
ted 'true' if both argument 1 and argument 2 are true. If argument 1 is false, the expression will =
be evaluated false and argument 2 will not be evaluated.=
p>
If argument 1 is true, Velocity Engine will then check =
the value of argument 2. If the value is true, the entire =
expression is true and Content 1 becomes the output. If the value =
is false then there will be no output as the entire expression is false. Logical OR operators work the same way as Logical AND, except for one of=
the references that needs to be evaluated 'true' for the entire expression=
to be considered true, for example: #set ($var1 =3D 1) $var3 < $var2 ) With Logical NOT operators, there is only one argument: To include some text immediately following the #else di=
rective, put curly brackets around the directive followed by the text to in=
clude, as shown in the example below:
#set ($var2 =3D 2)
#set ($var3 =3D 3)
#if( $var1 < $var3
Content 1
#end
To better understand the above example, let us assume that $var1 < $var=
3 is argument 1 and $var3 < $var2 is argument 2. According to the exampl=
e, argument 2 is false because $var2 is less than $var3 not the other way a=
round. Since argument 1 is true, Velocity Engine does not need to look at a=
rgument 2, whether argument 2 is true or false, the expression will be true=
, and Content 1 will be the output. Basically, a Logical OR operator requir=
es only one argument to be true (either argument 1 or argument 2) to make a=
n expression true. In the case that both argument 1 and argument 2 are fals=
e, there will be no output because the expression is false.#set ( =
$bool =3D false )=20
#if( !$bool )=20
Content 1=20
#end
As shown in the above example, !$bool is evaluat=
ed true and Content 1 is the output. But if $bool is true, then !$bool is false and=
there will be no output.#if( $f=
oo =3D=3D $bar)it is true!#{else}it is not!#end</li>