freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Daniel Dekany (JIRA)" <>
Subject [jira] [Commented] (FREEMARKER-76) New built-in ?if(condition, otherwise) to override left-hand expr. conditionally
Date Fri, 29 Sep 2017 07:30:00 GMT


Daniel Dekany commented on FREEMARKER-76:

I assume {{expensiveCall()?as("cached")!false}} really meant to be {{expensiveCall()!false?as("cached")}},
so the {{?if(cache)}} will work. Anyway, the use case seems to be a variation of this pattern:

<#if someExpensiveExpression??>
   Print something around ${someExpensiveExpression}.

which could be addressed as:

<#if someExpensiveExpression?as(x)??>
   Print something around ${x}.

It's longer than your version, but your is very strange because, apparently, it tries to print
something inside a string literal only so that it can use the {{?something}} syntax. That's
problematic, among others because of how auto-escaping works.

There's an issue with my above application of {{?as}} as well though... because of the fail-fast
behavior of FM2 with {{null}}-s, it's quite unusual that {{?as}} can continue even if its
left side is {{null}}, and much worse, in that case all I could implement is returning with
{{null}}, and not doing the assignment. Because storing {{null}} in a variable just would
make it transparent, that is, reading it would cause a fallback to a higher scope... (Yip,
there are reasons why FM3 was started.) Though in the above issue it doesn't mater, because
if it was {{null}}, then {{??}} will be {{false}}, and so you don't enter the {{if}} block
where {{x}} would be in scope.

> New built-in ?if(condition, otherwise) to override left-hand expr. conditionally
> --------------------------------------------------------------------------------
>                 Key: FREEMARKER-76
>                 URL:
>             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

View raw message