How to handle null
values in Freemarker? I get some exceptions in the template when null
values are present in data.
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
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 and
attribute are not null? –
Kitchenette 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"}
<dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.22</version></dependency>
to your pom.xml. –
Clo ${(object.dateAcquired?date)!"not present"}
–
Depurate ${salutation!'Dear Mr. or Mrs.'}
–
Niersteiner 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
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 and
attribute are not null? –
Kitchenette 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.
object
is nullable also then we can use (object.attribute)??
–
Spokane 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>
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.
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.
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:
- boolean need to convert to string by using "?c" see Built-ins for booleans
- but null we cannot convert to string and we need to use "!" see Handling missing values
user
is null? –
Kitchenette ${((user.isSuperman)!false)?c}
. Just test to be sure. –
Pademelon © 2022 - 2024 — McMap. All rights reserved.