Handling Zero Values ​​in Freemarker

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

+49
freemarker
Dec 19 '12 at 10:29
source share
4 answers

Can you use a testing operator ?? :

This checks to see if the object attribute is invalid:

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

This checks to see if the object or attribute is null:

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

Source: FreeMarker Guide

+60
Dec 19 '12 at 10:35
source share

Starting with freemarker 2.3.7, you can use this syntax :

 ${(object.attribute)!} 

or, if you want to display the default text if the attribute is null :

 ${(object.attribute)!"default text"} 
+52
Dec 23 '14 at 4:46
source share

I think it works differently.

 <#if object.attribute??> Do whatever you want.... </#if> 

If object.attribute not NULL, then the content will be printed.

+1
Aug 05 '14 at 15:12
source share

Use the operator ?? at the end of your <#if> statement.

This example shows how to handle null values ​​for two lists in a Freemaker template.

 List of cars: <#if cars??> <#list cars as car>${car.owner};</#list> </#if> List of motocycles: <#if motocycles??> <#list motocycles as motocycle>${motocycle.owner};</#list> </#if> 
0
Aug 27 '15 at 12:54
source share



All Articles