Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Velocity, what's the most efficient way to check if a string is empty and not null

Tags:

velocity

I often have cases when a string value is absent and/or empty. Is this the best way to test this condition?

#if( $incentive.disclaimer && $!incentive.disclaimer != '' ) 
   $incentive.disclaimer 
#end
like image 597
rsturim Avatar asked Oct 31 '12 12:10

rsturim


People also ask

How do I check if a string is empty or null?

You can use the IsNullOrWhiteSpace method to test whether a string is null , its value is String. Empty, or it consists only of white-space characters.

Which is better empty string or null?

So it is better to use empty string for database as allowing NULL value forces the system to do extra work and does not give the data that you are looking for. However, NULL does not throw any exception when the count() function is executed.

How do you check for null 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.

Which function is used to check an empty string?

Answer: Use the === Operator You can use the strict equality operator ( === ) to check whether a string is empty or not.


3 Answers

If you just want Velocity to display the value if there, or display nothing if absent, a quiet reference by itself will do the trick:

$!incentive.disclaimer

If you're wanting to explicitly test for empty, StringUtils from Apache Commons Lang can help. First add it to your Context (reference here):

context.put("StringUtils", StringUtils.class);

Though if you're on an older version of Velocity, it may not like the class reference, so you can add an instance instead:

context.put("StringUtils", new StringUtils());

Then you can call its isEmpty method from your Velocity template:

#if($StringUtils.isEmpty($incentive.disclaimer))
    ## logic here...
#end

If you want whitespace treated as empty, there's also isBlank.

like image 70
Evan Haas Avatar answered Sep 24 '22 08:09

Evan Haas


For cases where just $!incentive.disclaimer doesn't fit http://wiki.apache.org/velocity/CheckingForNull suggests a short solution:

#if( "$!car.fuel" != "" )
like image 26
Vadzim Avatar answered Sep 24 '22 08:09

Vadzim


You want Quiet Reference Notation: $!incentive.disclaimer

Bla bla $!incentive.disclaimer. 

If $incentive.disclaimer is null or "", Velocity will render:

Bla bla .

Refer to the official Guide section: https://velocity.apache.org/engine/devel/user-guide.html#quiet-reference-notation

Sometimes you do need #if

Most common case when you do want #if: your variable is just a part of a bigger piece of text and you don't want to show it if the variable is empty. Then you need this:

#if($incentive.disclaimer && !$incentive.disclaimer.empty) 
    Please read our incentive disclaimer:
    $incentive.disclaimer
#end
like image 16
DenisS Avatar answered Sep 28 '22 08:09

DenisS