Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Velocity nulls and empty strings

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 
like image 977
Jordi P.S. Avatar asked Sep 04 '12 12:09

Jordi P.S.


People also ask

What is the difference between a null string and an empty string?

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 .

What is better NULL or empty string?

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.

How do you find the NULL value of velocity?

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.

Is blank and empty string the same?

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.


1 Answers

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

like image 65
Irmak Cakmak Avatar answered Oct 07 '22 05:10

Irmak Cakmak