Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling null values in Freemarker

Tags:

freemarker

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

like image 594
Anand B Avatar asked Dec 19 '12 10:12

Anand B


People also ask

What is eval in FreeMarker?

eval. This built-in evaluates a string as an FTL expression. For example "1+2"? eval returns the number 3. (To render a template that's stored in a string, use the interpret built-in instead.)

How do you use the ternary operator in FreeMarker?

When applied to a boolean, the string built-in will act as a ternary operator. It's no very readable as this is not the intended usage of it. It's for formatting boolean values, like Registered: ${registered? string('yes', 'no')} .

How do I comment in FreeMarker template?

Comments: <#-- and --> Comments are similar to HTML comments, but they are delimited by <#-- and -->. Comments will be ignored by FreeMarker, and will not be written to the output.


2 Answers

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"} 
like image 154
Arnaud Avatar answered Oct 30 '22 08:10

Arnaud


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

like image 40
Tom Verelst Avatar answered Oct 30 '22 09:10

Tom Verelst