Alex Rivera | Logout

How to do an inline if/otherwise (aka ternary operator) in Velocity?

Asked 2011-02-15T14:40:51.420
43

In pure Java, I could do this:

value = (a > b) ? a : b;

Whereas in Velocity, the long form would be:

#if($a > $b)          
    #set($value = $a)
#else
    #set($value = $b)
#end

Is there a short form in Velocity? I want to be able to do an if/otherwise inline.

Edit
Report

1 Answer

61

You can do

#set($value = "#if($flag)red#{else}blue#end")
answered 2011-02-15T14:45:16.877

Your Answer