How do I compare numbers with a null check in freemarker?
freemarker
Solution
What do you want to happen if the variable is null? If you want then false:
<#if variable?? && variable == 2 ></#if>
If you want to assume 0:
<#if variable!0 == 2 ></#if>
Notes: `=` also works, but is a bad practice, as the expression can be confused with named parameter assignment. The `()` is redundant; it's not like in Java. `?html` meant to be used escaping `<` and such, but worse, you trigger localized formatting there that can spoil the comparison. So if you ever want to do something like that, use `?c` (*c*omputer formatting) instead.
Problem
What is the most elegant what of comparing numbers in freemarker? ``` <#if (variable = 2) ></#if> ``` This will not include a null check? If I do this ``` <#if (variable! = 2) ></#if> ``` Then freemarker will complain about different types. I ended up doing this ``` <#if (variable!?html = "2") ></#if> ``` But I guess this is not the way of doing it? How do I compare numbers with a null check in freemarker?