You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by "Ondra Žižka (JIRA)" <ji...@apache.org> on 2017/09/28 22:53:00 UTC
[jira] [Created] (FREEMARKER-76) New built-in ?if(condition,
otherwise) to override left-hand expr. conditionally
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)