Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ?? , has_content , if_exists in freemarker

Tags:

freemarker

what is the difference between the following in freemarker?

! has_content ??  if_exists 

I used ?? instead of has_content & it lead to such huge issues. Screwed up my day. I really need to get this thing clarified.

When I used

!(xyz.abc!)?? -- it dint work 

When I used

!(xyz.abc!)?has_content ... it did work  

Doesn't ?? OR has_content OR if_exists check for the same thing?

like image 224
user3606002 Avatar asked May 08 '14 20:05

user3606002


People also ask

How do I check if a template is null in FreeMarker?

The has_content Function 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.

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

What is FreeMarker language?

FreeMarker is a template engine, written in Java, and maintained by the Apache Foundation. We can use the FreeMarker Template Language, also known as FTL, to generate many text-based formats like web pages, email, or XML files.


1 Answers

?? tells if the left hand operand's value is missing (means it's Java null or you have an undefined variable there), and gives back false (missing) or true (not missing) accordingly.

?has_content is very much like ??, except it also returns false for a 0-length string or empty FTL sequence (like java.util.List, Java array, etc.) or empty FTL hash (like java.util.Map, etc.). (It doesn't return false for a 0, boolean false, etc.)

! is used to give a default value when a value is missing (again means that it's Java null or you have an undefined variable), like color!"no color". If you omit the right hand operand of !, then the default value is an empty string and empty sequence and empty hash on the same time (a multi-typed value), which is handy for writing things like <#list things! as thing>, ${foo!}.

?if_exists is the old way of writing ??. Don't use it.

While we are here, note that all these operators only cover the last step of a dotted or [] expression, like user.price!0 only handles if price is missing from user, but not if user itself is missing. To cover both possibilities, use (user.price)!0, which handles all missing variable errors thrown during the evaluation of the (), no mater where they come from.

like image 94
ddekany Avatar answered Oct 03 '22 20:10

ddekany