Unexpected variable scoping rules if #include is used within a #macro
"Newman, John W" <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <D688C4D318A63D4290AA94A33E06F7D20191D9B50B@msxmbxnsprd14.acct.upmchs.net> |
Hello,
We are using FM 2.3, and we've spotted a strange bug with our code. I've been wading through the docs for a bit here but haven't found the answer as to why this is happening. Perhaps a simple test is the best way to explain it:
Main template:
[#ftl]
[#import "scopeTest.ftl" as test /]
[@test.runTest /]
scopeTest.ftl:
[#ftl]
[#macro runTest]
[#list 1..30 as i]
[#include "strangeScope.ftl" /]
[/#list]
[/#macro]
strangeScope.ftl:
[#ftl]
[#if i % 10 == 0]
[#assign someLocal = i + 2 /]
[/#if]
${i} ${someLocal!'not defined'}<br />
Produces this output:
1 not defined
2 not defined
3 not defined
4 not defined
5 not defined
6 not defined
7 not defined
8 not defined
9 not defined
10 12
11 12 < What?
12 12 < Why is this still here
13 12 < shouldn't it say not defined, since, once the include reaches EOF the someLocal is out of scope and destroyed?
14 12
15 12
16 12
17 12
18 12
19 12
20 22 < correct
21 22 < this should say undefined??
22 22
23 22
24 22
25 22
26 22
27 22
28 22
29 22
30 32
Is this expected behavior? Why does a variable that is not declared in a macro remain in scope for the entire macro? [#local sounds like what I want but that is not permitted in the include. If the someLocal var was declared within the macro itself this is what I would expect, however in this case, I'm expecting it to be declared, destroyed, re-declared within the include. The macro shouldn't even know it is there. Right? Is this a bug or a feature? If it's the latter, please explain the reasoning here and what the best practice is if any.
Is it more correct to ditch the include, convert it to a second macro and use local? I'm a bit confused on this and concerned that we could have several other instances of include within macro...
Thanks as always.
------------------------------------------------------------------------------
Register Now & Save for Velocity, the Web Performance & Operations
Conference from O'Reilly Media. Velocity features a full day of
expert-led, hands-on workshops and two days of sessions from industry
leaders in dedicated Performance & Operations tracks. Use code vel09scf
and Save an extra 15% before 5/3. http://p.sf.net/sfu/velocityconf
_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user