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?
Like the Java programming language, Velocity has single-line and block comments. A single line comment begins with ## and finishes at the end of the line. This is an example of a single line comment in VTL: ## This is a single line comment.
The #macro directive allows you to name a section of a VTL template and re-insert it multiple times into the template.
Velocity Codes are attributed to a location. A velocity code associates the location to a SKU's throughput. For example, fast moving items are stored in the lower level locations, or locations reachable by hand, in the bulk zone.
Velocity is a Java-based templating engine. It's an open source web framework designed to be used as a view component in the MVC architecture, and it provides an alternative to some existing technologies such as JSP. Velocity can be used to generate XML files, SQL, PostScript and most other text-based formats.
From experience and reading the VTL Reference there is no way to do this. If you had lots of assignments like this maybe you could look at defining your own velocimacro to try and avoid repeating the if else.
For example, if the macro only prints a single string you could do the following:
#set ($name = "#condOpt($args.get(0), "default")")
The double quotes around the macro call are important as that means the RHS of the #set is parsed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With