Search code examples
freemarker

Boolean expression if false not working with ?? in freemarker


<#function kafkaCacheBackupLoaderProfiles>
  <#assign profiles = []>
  <#if featureEnabled.createCacheLoader??>
    <#assign profiles = profiles + ["kafka-cache-loader"]>
  </#if>
  <#if featureEnabled.createBackupLoader!false>
    <#assign profiles = profiles + ["kafka-backup-loader"]>
  </#if>
<#return profiles?join(", ")> 

for the input following inputs :

"featureEnabled": {
   "createCacheLoader": true
}

the code returns: kafka-cache-loader

"featureEnabled": {
            
}

the code returns : <an empty profiles array>

"featureEnabled": {
   "createCacheLoader": false
}

the code returns : kafka-cache-loader

  1. it should return <an empty profiles array> as createCacheLoader is false
  2. I tried with <#if (featureEnabled.createCacheLoader)??> but the result is same as shown above.

if we talk about the other expression: <#if featureEnabled.createBackupLoader!false> code is working as expected i.e incase createBackupLoader is false or null it does not print kafka-backup-loader . if true it prints kafka-backup-loader

I think I am missing some concept here.


Solution

  • The featureEnabled.createCacheLoader?? expression just tells if createCacheLoader (inside featureEnabled, which must exist) is existing and non-null, and it doesn't matter what the value of it is. If you want it to be false in effect if it's not there, then write featureEnabled.createCacheLoader!false.

    See also: https://freemarker.apache.org/docs/dgui_template_exp.html#dgui_template_exp_missing