Search code examples
freemarker

Handling null values in Freemarker


How to handle null values in Freemarker? I get some exceptions in the template when null values are present in data.


Solution

  • You can use the ?? test operator:

    This checks if the attribute of the object is not null:

    <#if object.attribute??></#if>
    

    This checks if object or attribute is not null:

    <#if (object.attribute)??></#if>
    

    Source: FreeMarker Manual