The Velocity if element is used to put forth a conditional test in Velocity.  See the Apache Velocity If - ElseIf section for details

Conditions:

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 )

The code below will only output the title and artist elements of the CDs that has a price that is higher than 10.


<h2>My CD Collection</h2>
<table style="border: 3px solid #450084">
<tr style="background:#ccc999">
<th style="text-align:left">Title</th>
<th style="text-align:left">Artist</th>
</tr>
#set($cds = $_XPathTool.selectNodes($contentRoot, '//catalog/cd'))
#foreach($cd in $cds) ##LOOP
#set ($artist = $cd.getChild('artist').value)
#set ($title = $cd.getChild('title').value)
#set ($pricetext = $cd.getChild('price').value)
#set ($price = $_MathTool.toDouble($pricetext) )
#if( $price > 10 )
<tr>
<td>$title</td>
<td>$artist - $pricetext</td>
</tr>
#end ##IF
#end ##LOOP
</table>



My CD Collection

Title Artist
Empire Burlesque Bob Dylan - 10.90
Still got the blues Gary Moore - 10.20
One night only Bee Gees - 10.90
Romanza Andrea Bocelli - 10.80
Black angel Savage Rose - 10.90
1999 Grammy Nominees Many - 10.20


Back to Top