??, ?has_content, ! in ftl language in freemarker , the difference between ?if_exists

Recently, there was a bug in the project. At the beginning, ?? was used to judge whether it was empty, but an error was reported, and it was correct to change it to ?has_content.

After looking up the reason on the Internet, the summary is as follows:
Original answer address:
https://stackoverflow.com/questions/23552151/difference-between-has-content-if-exists-in-freemarker

?? 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 an empty string or empty list or empty map. (It doesn’t return false for a 0, boolean false, etc.)

! is used to give a default value when a value is missing, 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.

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

The general idea is as follows:
?? Determine whether the variable on the left is missing, which is equivalent to the judgment of null in java, or whether the variable is undefined.
?has_content is very similar to ??, but it also evaluates to an empty string or an empty list or an empty map.
! is used to give a default value to a variable, such as color!”no color”
?if_exists is the old way of writing ??, which is now deprecated.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326378264&siteId=291194637