Velocity nulls and empty strings

Jordi P.S. picture Jordi P.S. · Sep 4, 2012 · Viewed 46.9k times · Source

In velocity I have a variable which its value is null. I don't want to display anything in that case.

Currently the template engine translates "" to null so I have to do.

#set ( $a = "")
#if ($a) 
   assert("never prints a neither gets here: " + $a)
#end

Is there a way I could do that directly? I'd like to be able to make something like:

This is the variable $a. ## in case that $a is null i don't want 'dollar a' to be displayed

Answer

Irmak Cakmak picture Irmak Cakmak · Sep 4, 2012

$!a does the trick. You can use this form directly without an if check.