[ 
https://issues.apache.org/jira/browse/FREEMARKER-21?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15864007#comment-15864007
 ] 

Pedro M. Zamboni commented on FREEMARKER-21:
--------------------------------------------

bq. \[…] I know pretty much all the unspeakable horrors of FM2, but that it 
throws an exception there \[…] is not among them \[…]

Okay, maybe I’m just not “thinking Freemarker” enough yet, then 😜.

However, I do still have an idea about how to better handle absent values in 
Freemarker.

bq. \[…] {{(...)!exp}} is basically an {{InvalidReferenceException}} exception 
handler facility \[…]

That’s really interesting. It seems that it is, indeed, able to handle 
exceptions that happened anywhere in the execution of the left‐hand‐side. For 
example, the following _does_ output “Goodbye!”.

{code:none}
<#function foo>
    ${absent}
    <#return "Hello!">
</#function>

${(foo())!"Goodbye!"}
{code}

Makes me wonder how {{has_content}} works. Since {{absent}} would throw an 
exception (if there is no variable named “{{absent}}”), the {{has_content}} 
part in {{absent?has_content}} would never have a chance to execute.

bq. \[…] and that's something I do want to change in FM3 \[…]

I think I have a solution that you might like. It doesn’t allow you to 
dangerous things (such as outputting an absent value or storing it in a 
variable), but it would also mean that {{!}} and {{??}} would not be able to 
handle exceptions that might have happened deep inside the function call stack 
(for that you should be using {{attempt}}/{{recover}} anyway).

I’d love to post the suggestion here, but…

bq. FM3 related stuff should be discussed on the dev list.

So I’ll post it there.

Thanks for your attention 😃!

> Reference to the object in condition - avoid repeating the same in #if and 
> ${...}
> ---------------------------------------------------------------------------------
>
>                 Key: FREEMARKER-21
>                 URL: https://issues.apache.org/jira/browse/FREEMARKER-21
>             Project: Apache Freemarker
>          Issue Type: New Feature
>          Components: engine
>            Reporter: Ondra Žižka
>
> Repeating stuff is annoying, especially in programming.
> Therefore this is annoying:
> {code}
> <#if someVariable.someMethod.someProperty??>
> <div>${someVariable.someMethod.someProperty}</div>
> </#if>
> {code}
> This would be better:
> {code}
> <#if someVariable.someMethod.someProperty?? as value>
> <div>${value}</div>
> </#if>
> {code}
> Not talking about the fact that it could only be evaluated once.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to