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

后端 未结 3 1556
我寻月下人不归
我寻月下人不归 2020-12-16 10:02

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         


        
3条回答
  •  死守一世寂寞
    2020-12-16 10:29

    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
    

提交回复
热议问题