This is a piece of my freemarker template:
${order.needByDate?if_exists?date}
I want it to work as following:
needByDate
is null, then write nothingThe above works only in second scenario. What is the correct way to achieve this?
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.
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!
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.)
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.
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"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With