Dear Daniel,

Thank you so much for your kind inputs.
Passing back values in nested works for me.

Please see my comments inline.

On Sat, Nov 18, 2017 at 12:50 AM, Daniel Dekany <[email protected]> wrote:

> Friday, November 17, 2017, 3:55:22 PM, Daniel Dekany wrote:
>
> > #assign sets the variable in the current namespace. Each #include-d
>
> I wanted to write #import"-ed above... #include-d templates use the
> namespace of the includer.
>
> > 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.)
>

My bad, I used wrong example here (*getName*), actually I want multiple
values for #nested code.
like

Defining macro
<#macro linkInfo>
  <#local name = "FreeMarker World"/>
  <#local url = "https://freemarker.apache.org/"/>
  <#nested name, url>
</#macro>

Using the defined macro

<#import "component://hwmapps/template/commerce/templates/Widgets.ftl" as
e/>
<@e.linkInfo ; name, url>
  <a href="${url!}">${name!}</a>
</@e.linkInfo>


So, in my case, I think, macro is needed instead of function.


>
> > 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
>
>
Thanks again for sharing your time.
P.S. Apologies for late reply, I was out of town :-)

- Best Regards,
Swapnil M Mane

Reply via email to