How to set null to a variable in freemarker
Depending on what you need it for, you can use a different type to indicate a "missing" value.
For instance, if you have myVariable
that is normally a number, assign false
to it, and then instead of checking myVariable??
, check myVariable!false?is_number
. This will cover both cases (non-existent and "unset").
${ (myVariable!false?is_number)?c }
<#assign myVariable = 12 >
${ (myVariable!false?is_number)?c }
<#assign myVariable = false >
${ (myVariable!false?is_number)?c }
Result:
false
12
false
Go try.
No, there's no "unassign", nor the concept of null
exists in FreeMarker (until 2.4.0 at least, but that's far away anyway). It only have missing variables (maybe technically a null
, maybe doesn't exist at all) and those that are there. I don't really get why is that needed in your case. Can you show a simplified example of the situation?
You could assign an empty string to your variable and check with the buit-in ?has_content
if it is set:
${hi?has_content?then(hi, "bye")}
<#assign hi="hi">
${hi?has_content?then(hi, "bye")}
<#assign hi="">
${hi?has_content?then(hi, "bye")}
This will render:
bye
hi
bye