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
An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .
However, it will throw an error of unique constraint if you use an empty string here. So, NULL is better. An empty string is useful when the data comes from multiple resources. NULL is used when some fields are optional, and the data is unknown.
If you're using velocity for AWS API Gateway, you need #if($car. fuel == "") to check for null in the response. The implicit approach ( #if($car. fuel) ) will not work.
An empty string is a String object with an assigned value, but its length is equal to zero. A null string has no value at all. A blank String contains only whitespaces, are is neither empty nor null , since it does have an assigned value, and isn't of 0 length.
$!a
does the trick. You can use this form directly without an if
check.
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