Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

The documentation is here
http://velocity.apache.org/engine/releases/velocity-1.7/user-guide.html

An if statement looks like this

    #if( $user.userId )
        ${user.userId}
    #end

An if-elseif-else looks like this

#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

A for loop looks like: $velocityCount is a 1-based counter of which element you are on in the loop.

     #foreach( $transaction in $transactionList )
          $!transaction.fullName
          $velocityCount
     #end

Values can be output by ${…} or $… There is a particular case where the ${…} notation should be used, but the case eludes me right now.

     ${data.firstName}
     $data.firstName

If the value of ${data.firstName} does not exist, then ${data.firstName} will be outputted. To get around that, use the $!… notation. Then if it doesn’t exist, nothing will be outputted.

$!transaction.fullName

Setting a variable during the processing of the template is like

#set( $rowCss = "odd")

<tr class="$!rowCss">
</tr>

Setting an array variable is very similar to setting a regular variable.

#set($platformsClasses = ['desktop','mobile bxslider'] )

Iterating over the array variable is the same as it is for any iterable item.

        #set($platformsClasses = ['desktop','mobile bxslider'] )
		#foreach($platformClass in $platformsClasses)
            <ul class="$platformClass">
                /* ... */
            </ul>
		#end

February 20th, 2014

Posted In: java ninja, Javaninja, templating, velocity

Tags:

Leave a Comment

LinkedIn Auto Publish Powered By : XYZScripts.com