How to use nested if condition in velocity template template?

3.7k Views Asked by At

I'm trying to create a nested if condition using velocity template. I am not sure if it is right. Please give the suggestions if it works or not..!!

#if(myExpression1)
#if(myExpression2)
 Result 1
#else
  Result 2
#end
#else
  Result 3
#end
2

There are 2 best solutions below

0
On

We can do all kinds of conditional operations just an example here.

#if ( $ctx.isGuest() )
        #set ($orderInfoUrl = "${ctx.baseUrl}/guest/order/${ctx.orderGuid}")
    #else
        #set ($orderInfoUrl = "${ctx.baseUrl}/my-account/order/${ctx.orderCode}")
    #end

0
On

Yes this works. You can nest an if within an if. Just make sure that all #if statements are closed with an #end

There are some examples of nested statements within default hybris. For example in copyable-template.vm

#if ($hasEqualsProperties)
    <code>
    #if ($superEquals)
        <code>
    #end
    <code>
    #foreach($v in $equalsProperties)
        #if ($v.type == 'boolean')
            <code>
        #else
            <code>
        #end
    #end
#end

For simplicity, I've removed some of the code and only left the # statements in