Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. --
like image 996
Samarland Avatar asked Oct 29 '25 10:10

Samarland


1 Answers

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>
like image 156
user7294900 Avatar answered Oct 31 '25 01:10

user7294900



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!