Velocity nulls and empty strings

后端 未结 3 1666
一生所求
一生所求 2020-12-15 17:14

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

3条回答
  •  有刺的猬
    2020-12-15 17:31

    Another alternative is to modify your if statement per Checking for Null (thanks for the link @xavi-lópez):

    Approach 2: Use the fact that null is evaluated as an empty string in quiet references. (cf. http://velocity.apache.org/engine/devel/user-guide.html#quietreferencenotation)

    So, your code would be:

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

提交回复
热议问题