Freemarker - Check Boolean value

13,843

Freemarker has then function since version 2.3.23:

<@displayRow label="Receiving Stores" value="${form.allStores?then('All Stores', 'No Stores')}"/>

Used like booleanExp?then(whenTrue, whenFalse)

Also similar to java, you can use the ! operator to negate:

<#if !form.allStores> 
    <@displayRow label="Receiving Stores" value="No Stores"/>

Then boolean can be only true/false so don't need elseif :

<#else>
    <@displayRow label="Receiving Stores" value="All Stores" />
</#if>

Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

Also prefer to use first positive condition as:

<#if form.allStores> 
    <@displayRow label="Receiving Stores" value="All Stores" />
<#else>
    <@displayRow label="Receiving Stores" value="No Stores"/>
</#if>
Share:
13,843
Samarland
Author by

Samarland

Updated on June 04, 2022

Comments

  • Samarland
    Samarland about 2 years

    What's the right syntax to check if the value of boolean from the form data in FreeMarker, my code:

    <#if "${form.allStores}" !false>
            <@displayRow label="Receiving Stores" value="All Stores" />
        <#elseif "${form.storesReceiving}" == false || "${form.storesReceiving}"?has_content>
            <@displayRow label="Receiving Stores" value="No Stores"/>
        <#else>
    

    I'm getting this error:

    Could not prepare mail; nested exception is freemarker.core._MiscTemplateException: Can't convert boolean to string automatically, because the "boolean_format" setting was "true,false", which is the legacy default computer-language format, and hence isn't accepted. --