Nullable date in Freemarker
-
18-09-2019 - |
Question
This is a piece of my freemarker template:
${order.needByDate?if_exists?date}
I want it to work as following:
- if
needByDate
is null, then write nothing - if it is not null, then write the date part
The above works only in second scenario. What is the correct way to achieve this?
Solution
There may be a smarter way of doing this but the following should do the job.
<#if order.needByDate??>${order.needByDate?date}</#if>
OTHER TIPS
This should also work
${(order.needByDate?date)!}
The parentheses are necessary
You can also add a default value such as "n/a" like this
${(order.needByDate?date)!"n/a"}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow