Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nullable date in Freemarker

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?

like image 243
Ula Krukar Avatar asked Dec 09 '09 09:12

Ula Krukar


People also ask

How do you handle null in FreeMarker?

FreeMarker has built-in functions to detect for variables. The most common method of detecting for empty or null values uses the has_content function and the trim function. The has_content function is often used with another built-in FreeMarker function to detect for null values.

How do I set default value in FreeMarker?

FreeMarker: !: Set default value to missing or null values By default, FreeMarker throws an error whenever it encounters a missing or null value. If you want to handle these null values, you should convey to FreeMarker explicitly. We can specify default value to a variable using ! unsafe_expr!

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.)

Is FreeMarker case sensitive?

The string converted to boolean value. The string must be true or false (case sensitive!), or must be in the format specified by the boolean_format setting. If the string is not in the appropriate format, an error will abort template processing when you try to access this built-in.


1 Answers

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"}
like image 114
Glenn Lawrence Avatar answered Oct 17 '22 11:10

Glenn Lawrence