Handling null values in Freemarker
Asked Answered
C

6

105

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

Commove answered 19/12, 2012 at 10:29 Comment(1)
Possible Duplicate: #307232Fibrilla
C
118

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

Carlo answered 19/12, 2012 at 10:35 Comment(6)
What is the difference between this approach and has_content ??Commove
has_content, next to null-checking, also checks if the value is not empty. This works for strings, sequences, hashes or collections. If the object is a date, boolean or a number, then it acts as non-empty. For all other types it will act as empty.Carlo
I have this problem where I have to check if the value in bean is null.I tried the following:${checknull(Bean.getValue())}, where checknull if a function <#function checknull x> <#if x??> <#return ""> <#else> <#return x> </#if> </#function> but I get "Error executing macro: checknull required parameter: x is not specified." errorCommove
You have to reverse the returns: <#function checknull x> <#if x??> <#return x> <#else> <#return ""> </#if> </#function>Carlo
Please check below answer: https://mcmap.net/q/204064/-handling-null-values-in-freemarkerHoral
@TomVerelst what if I want to check if both the object and attribute are not null?Kitchenette
C
138

Starting from freemarker 2.3.7, you can use this syntax :

${(object.attribute)!}

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

${(object.attribute)!"default text"}
Commensurable answered 23/12, 2014 at 4:46 Comment(5)
For those who are using Freemarker as template engine for XDocReport, this is working after adding <dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.22</version></dependency> to your pom.xml.Clo
How do you do it if you have a date and the date could be null? In other words object.dateAcquired where dateAcquired could be null and you have ${object.dateAcquired?date}Bundesrat
@StephaneGrenier use ${(object.dateAcquired?date)!"not present"}Depurate
Great example. Helped me alot. The bracket, however are obsolet. ${salutation!'Dear Mr. or Mrs.'}Niersteiner
Any idea how could I print .now?long on default value?Flagitious
C
118

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

Carlo answered 19/12, 2012 at 10:35 Comment(6)
What is the difference between this approach and has_content ??Commove
has_content, next to null-checking, also checks if the value is not empty. This works for strings, sequences, hashes or collections. If the object is a date, boolean or a number, then it acts as non-empty. For all other types it will act as empty.Carlo
I have this problem where I have to check if the value in bean is null.I tried the following:${checknull(Bean.getValue())}, where checknull if a function <#function checknull x> <#if x??> <#return ""> <#else> <#return x> </#if> </#function> but I get "Error executing macro: checknull required parameter: x is not specified." errorCommove
You have to reverse the returns: <#function checknull x> <#if x??> <#return x> <#else> <#return ""> </#if> </#function>Carlo
Please check below answer: https://mcmap.net/q/204064/-handling-null-values-in-freemarkerHoral
@TomVerelst what if I want to check if both the object and attribute are not null?Kitchenette
C
5

I think it works the other way

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

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

Cortie answered 5/8, 2014 at 15:12 Comment(1)
If object is nullable also then we can use (object.attribute)??Spokane
L
1

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

This example demonstrates 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>
Leprosarium answered 27/8, 2015 at 12:54 Comment(0)
H
1

I would like to add more context if you have issues and this is what I have tried.

<#if Recipient.account_type?has_content>
  … (executes if variable exists)
<#else>
  … (executes if variable does not exist)
</#if>

This is more like Javascript IF and ELSE concept where we want to check whether this value or another chaining through required logic.

Freemarker webite

Other reference:

Scenario: Customer has ID and name combined like 13242 Harish, so where our stakeholder need the only name, so I have tried this ${record.entity?keep_after(" ")} and it did work, however, it can only work when you have space, but when a customer doesn't have space and one name, I had to do some IF ELSE condition to check Null value.

Hendren answered 10/6, 2021 at 19:23 Comment(0)
P
1

Use:

${(user.isSuperman!false)?c}

It will work when "isSuperman" is null and when have boolean value

What we want:

  • For "null" we want to output 'false'
  • For boolean value and want to output value ('true' or 'false')

What we know:

Pademelon answered 7/5, 2022 at 7:7 Comment(3)
Here what happens if the user is null?Kitchenette
@bee I think it will be an exception and I suggest we need to use extra circle brackets as ${((user.isSuperman)!false)?c}. Just test to be sure.Pademelon
Yes, that's what's worked. Thanks.Kitchenette

© 2022 - 2024 — McMap. All rights reserved.