freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ondra Žižka (JIRA) <j...@apache.org>
Subject [jira] [Created] (FREEMARKER-76) New built-in ?if(condition, otherwise) to override left-hand expr. conditionally
Date Thu, 28 Sep 2017 22:53:00 GMT
Ondra Žižka created FREEMARKER-76:
-------------------------------------

             Summary: New built-in ?if(condition, otherwise) to override left-hand expr. conditionally
                 Key: FREEMARKER-76
                 URL: https://issues.apache.org/jira/browse/FREEMARKER-76
             Project: Apache Freemarker
          Issue Type: New Feature
          Components: engine
    Affects Versions: 2.3.26-incubating
            Reporter: Ondra Žižka


I propose this built-in:
{code}
expr?if(condition, otherwise)
{code}
This would check condition, and if true, it would pass along the {{expr}}
If condition was false, it would pass along the {{otherwise}}.
The diffrence from {{condition?then(expr, otherwise)}} is that {{expr}} would be evaluated
even if  {{condition}} was false. The otherwise could be optional and default to "".

For instance,
{code}
${ "Hi, ${user}!"?if(user??) }
{code}

The true benefit of {{?if}} would be when used with {{as?}}, proposed in FREEMARKER-75.  

{code}
${ "Hi, ${expensiveCall()?as("cached")!false}, again!"?if(cached) }
{code}

Without these goodies, this would look like this:

{code}
<#assign cached = expensiveCall() >
<#if cached?has_content >
    Hi, ${ cached }, again!
</#if>
{code}

I hope I didn't overlook some other obvious solution :)



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Mime
View raw message