#assign sets the variable in the current namespace. Each #include-d
template has its own namespace, plus the the topmost template has its
own namespace (the "main" namespace). So if you use #assign like that,
then in the other namespace you had to write ${widget.name}. You might
want to use #global instead, and then the variable is visible from
everywhere (unless it's shadowed by a variable of the same name in the
same namespace). (It's also possible to pass back a value like
<#nested "FreeMarker World"> and then
<@widget.getName ; name>${name}</@widget.getName>. Also you should write
a #function if you just want to get a value, not a macro, as that has
a return value.)

See also: https://freemarker.apache.org/docs/dgui_misc_namespace.html


Friday, November 17, 2017, 1:56:03 PM, Swapnil Mane wrote:

> Dear FreeMarker team,
>
> First I would like to thank you for creating such a nice template engine, I
> personally liked it very much :-)
>
> I need your help in an issue,
>
> While using <#nested>, the variable I defined using assign is not
> accessible in nested code.
>
> Here is the more details
>
> 1.) I have a macro file having Widgets.ftl
>
> {code}
>
> <#macro getName>
>     <#assign name = "FreeMarker World"/>
>     <#nested>
> </#macro>
>
> {code}
>
> 2.) I have included the above file in another file using <#import>
>
> {code}
>
> <#import "Widgets.ftl" as widget>
> <@widget.getName>
>     Hi ${name!}
> </@widget.getName>
>
> {code}
>
> Expected output - Hi FreeMarker World
> Actual output - Hi
>
>
> Here the value of 'name' variable is not setting properly if assign is used.
> But when I set it using global, everything work as expected.
>
> {code}
> <#global name = "FreeMarker World"/>
> {code}
>
> The similar types of macros will be heavily used in my application and
> setting it at global level will cause performance and memory issue. So, I
> want to set value of variable using assign.
>
> Please let me know if I am missing anything.
> Thanks again for your time and contribution.
>
> - Best Regards,
> Swapnil M Mane

-- 
Thanks,
 Daniel Dekany

Reply via email to