freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Daniel Dekany (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (FREEMARKER-89) Template rendering gets confused with static functinos
Date Sat, 17 Feb 2018 17:24:00 GMT

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

Daniel Dekany commented on FREEMARKER-89:
-----------------------------------------

It's by design, and such can't be fixed, at least not in 2.x. Unlike in Java, there's no
separate namespace for methods and "fields" (JavaBean properties actually) in the template
language. So yes, the method will shadow the JavaBean property of the same name; {{sample.data()}} just
means {{(sample.data)()}}, thus the presence or lack of {{()}} doesn't influence how the
name ("data") is resolved.

So, yes, that's what you can do for now. Or you can write {{$\{sample.getData()}}}.

(In FM3 I wonder if we should have a separate method and non-method namespace... it has it's
pros and cons. Especially nowadays... like AFAIR Scala didn't have that separation either.)

Can we close this?

> Template rendering gets confused with static functinos
> ------------------------------------------------------
>
>                 Key: FREEMARKER-89
>                 URL: https://issues.apache.org/jira/browse/FREEMARKER-89
>             Project: Apache Freemarker
>          Issue Type: Bug
>          Components: engine
>    Affects Versions: 2.3.23, 2.3.27-incubating
>            Reporter: Shawn Dempsay
>            Priority: Major
>
> I have an immutable class that shares naming for a field and a static getter:
> package org.dempsay.demo.freemaker.api;
>  
>  
> {code:java}
> public class Sample {
>     public final String data;
>     protected Sample(final SampleBuilder builder) {
>         this.data = builder.data;
>     }
>     public static String data(final Sample sample) {
>         return sample.data;
>     }
> }
> {code}
>  
> When I render it with the following template:
> {code:java}
> The data from this sample is ${sample.data}
> {code}
> I get this error:
> Feb 17, 2018 7:59:40 AM freemarker.log._JULLoggerFactory$JULLogger error
> SEVERE: Error executing FreeMarker template
> FreeMarker template error:
> For "${...}" content: Expected a string or something automatically convertible to string
(number, date or boolean), or "template output" , but this has evaluated to a method+sequence
(wrapper: f.e.b.SimpleMethodModel):
> ==> sample.data  [in template "sample.ftl" at line 1, column 32]
>  
> ----
> Tip: Maybe using obj.something(params) instead of obj.something will yield the desired
value
> ----
>  
> ----
> FTL stack trace ("~" means nesting-related):
> - Failed at: ${sample.data}  [in template "sample.ftl" at line 1, column 30]
> ----
>  
> The full code for this is posted in github at: https://github.com/sdempsay/freemarker-bug
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message