I think a lot of people are wanting to be able to check to see if their variable is not empty as well as if it exists. I think that checking for existence and emptiness is a good idea in a lot of cases, and makes your template more robust and less prone to silly errors. In other words, if you check to make sure your variable is not null AND not empty before using it, then your template becomes more flexible, because you can throw either a null variable or an empty string into it, and it will work the same in either case.
<#if p?? && p?has_content>1</#if>
Let's say you want to make sure that p
is more than just whitespace. Then you could trim it before checking to see if it has_content
.
<#if p?? && p?trim?has_content>1</#if>
UPDATE
Please ignore my suggestion -- has_content
is all that is needed, as it does a null check along with the empty check. Doing p?? && p?has_content
is equivalent to p?has_content
, so you may as well just use has_content
.