Search code examples
freemarker

How can I check null and empty both in freemarker string?


For example. String s can have these values like "value", "" or null.

<#if str?? && str?has_content>
    ${str}
</#if>

Can I check ??(null) and ?has_content(empty not null) both value in freemarker if statement not using TemplateModel?


Solution

  • str?has_content returns true if str is non-null (non-missing), and is also not a 0-length string. So you just need <#if str?has_content>.

    (As of TemplateModel-s, every value is a TemplateModel as far as templates see. There's no such thing as a non-TemplateModel value.)