You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by "Daniel Dekany (JIRA)" <ji...@apache.org> on 2017/09/18 08:07:00 UTC

[jira] [Commented] (FREEMARKER-72) Import 2 different files which include 1 same file makes vars the same

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

Daniel Dekany commented on FREEMARKER-72:
-----------------------------------------

Yeah, I have ran into that old gem when reading the source code quite recently... (Technical details: There's an {{Environment}}-scope ({{Template.process(...)}}-scope) {{Map}} that associates each macro definition (the AST node) to the namespace where {{#macro}} was executed in. So in the second import, because the macro definition node is exactly the same (the same {{Template}} object is reused), the macro is re-associated to the 2nd namespace. For 12 years, nobody has reported this, now suddenly we both notice it.) I'm afraid it can't be fixed without breaking backward compatibility too much... I will take a second look though. (I have fixed it easily in the FreeMarker 3 branch, but that won't help users any time soon.) A workaround is using {{#import}} instead if {{#include}} to pull in the shared logic of p1 and p2. In case you really need to access the namespace of the caller, you will have to pass {{.namespace}} as argument to the macro though ({{<#macro showId callerNS>$\{callerNS.id\}</#macro>}}, and then where you call it {{<@shared.showId .namespace>}}).

> Import 2 different files which include 1 same file makes vars the same
> ----------------------------------------------------------------------
>
>                 Key: FREEMARKER-72
>                 URL: https://issues.apache.org/jira/browse/FREEMARKER-72
>             Project: Apache Freemarker
>          Issue Type: Bug
>          Components: engine
>    Affects Versions: 2.3.26-incubating
>            Reporter: Josh Wang
>
> Four Files:
> *p1.ftl*
> {code:java}
> <#assign id="p1"><#include "macro.ftl">
> {code}
> *p2.ftl*
> {code:java}
> <#assign id="p2"><#include "macro.ftl">
> {code}
> *macro.ftl*
> {code:java}
> <#macro showId>${id}</#macro>
> {code}
> *index.ftl*
> {code:java}
> <#import "p1.ftl" as p1><@p1.showId /><#-- @showId == p1 -->
> <#import "p2.ftl" as p2><@p2.showId /><#-- @showId == p2 -->
> <@p1.showId /><#-- Why @showId == p2 ??? -->
> {code}
> First call <@p1.showId /> show the right result. But after call <@p2.showId />, if you call <@p1.showId /> again, the result is wrong.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)