Tuesday, February 28, 2012

Handling null values in freemarker using if condition

From: http://stackoverflow.com/questions/306732/how-to-check-if-a-variable-exists-in-a-freemarker-template

Two solutions:

First Solution:
Example 1:
<#if userName??>
   Hi ${userName}, How are you?
</#if>
Example 2:
<#if router.cableFacility.direction??>
   Hi ${router.cableFacility.direction}, How are you?</#if>
Second Solution:
Hi ${userName!}, How are you?
the default operator also supports a default value, such as:
Hi ${userName!"John Doe"}, How are you?

No comments: