[
https://issues.apache.org/jira/browse/FREEMARKER-75?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16185750#comment-16185750
]
Daniel Dekany edited comment on FREEMARKER-75 at 9/29/17 12:28 PM:
-------------------------------------------------------------------
Regarding that {{?as}} should replace an existing variable. Maybe the sane way
of combining this with FM2 is that {{?as\(x)}} declares a nested content
variable (aka a loop variable), similarly to {{#list xs as x}} does. Otherwise
the scope is not obvious, like, if you have {{$\{expensiveExpression?as\(x)\}}}
inside {{#function}}, will {{x}} be a local variable, or a template namespace
variable? (In FM3 I plan to use {{#var}} and {{#set}}, where the scope of the
last depends on where {{#var}} was.) Anyway, this last doesn't exactly helps
the readability of the template ("Where does x coming from?" Looks for
{{#assign}}-s for no avail...), so I probably wouldn't do that it in FM3
either. The case where {{?as\(x)}} declares a nested content variable has a
strong use case with {{#if}}, and the scope of the variable is restricted,
which hopefully makes this less confusing.
was (Author: ddekany):
Regarding that {{?as}} should replace an existing variable. Maybe the sane way
of combining this with FM2 is that {{?as(x)}} declares a nested content
variable (aka a loop variable), similarly to {{#list xs as x}} does. Otherwise
the scope is not obvious, like, if you have {{$\{expensiveExpression?as(x)\}}}
inside {{#function}}, will {{x}} be a local variable, or a template namespace
variable? (In FM3 I plan to use {{#var}} and {{#set}}, where the scope of the
last depends on where {{#var}} was.) Anyway, this last doesn't exactly helps
the readability of the template ("Where does x coming from?" Looks for
{{#assign}}-s for no avail...), so I probably wouldn't do that it in FM3
either. The case where {{?as(x)}} declares a nested content variable has a
strong use case with {{#if}}, and the scope of the variable is restricted,
which hopefully makes this less confusing.
> New built-in ?as("variableName") to store the left-hand operand to a variable.
> ------------------------------------------------------------------------------
>
> Key: FREEMARKER-75
> URL: https://issues.apache.org/jira/browse/FREEMARKER-75
> Project: Apache Freemarker
> Issue Type: New Feature
> Components: engine
> Affects Versions: 2.3.26-incubating
> Reporter: Ondra Žižka
>
> When coding templates, one often runs into a situation like this:
> {code}
> <#if (foo.bar.baz.expensiveCall())!false >
> ${ foo.bar.baz.expensiveCall() }
> </#if>
> {code}
> Solving it with currently available things is something like
> {code}
> <#assign cached = (foo.bar.baz.expensiveCall())! >
> <#if cached?has_content() >
> Hello, ${ cached }
> </#if>
> {code}
> In short, passing around the information that a value could not be obtained
> is a bit cumbersome.
> What I suggest is a built-in that would capture a value of an expression on
> the left to a new variable and pass it along.
> {code}
> <#if (foo.bar.baz.expensiveCall())!?as("cached")?has_content >
> Hello, ${ cached }
> </#if>
> {code}
> The benefits is that there is need to compute the expression twice or do the
> assignment.
> Speaking of simplifying, the expression above could also be simplified by
> another built-in, a "reversed" {{then()}}. (For that I will file another
> jira.)
> {code}
> ${ "Hi, "?if( expensive()?as("cached"), "") + cached } // For conditional
> prefixes
> ${ expensive()?as("cached")!false + ", good bye!"?if(cached) } // For
> conditional suffixes
> ${ "Hi, ${expensive()?as("cached")!false}, again!"?if(cached) } // For
> conditional surrounding
> {code}
> The new {{?as()}} built-in could be also used for a more convenient way of
> assigning wile printing:
> {code}
> ${"Hi ${user}"?as("greet")}.
> ${ greet }, again.
> {code}
> I hope my explanation does make sense.
> Thanks for considering.
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)