'conditional operator in Velocity
Is there a way to do ternary operators in Velocity? This is what I'd like to do:
#set ($name = ($args.get(0) == "") ? "default" : $args.get(0))
Instead of chunky if-else
#if ($args.get(0) == "")
#set ($name = "default")
#else
#set ($name = $args.get(0))
#end
Any ideas?
Solution 1:[1]
I ended up doing as you said, Mark:
#macro(condOp $check, $default)
#if ($check == "")
$default
#else
$check
#end
#end
And then I can call it like so:
#set ($name = "#condOp($args.get(0), 'default')")
Solution 2:[2]
For the record, with Velocity 2.1+, you can provide alternate reference values:
${args[0]|'default'}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | peirix |
| Solution 2 | Claude Brisson |
