Samarland
Samarland

Reputation: 581

Freemarker - Check Boolean value

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. --

Upvotes: 3

Views: 25184

Answers (1)

Ori Marko
Ori Marko

Reputation: 58774

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>

Upvotes: 8

Related Questions