Discussion:
[FreeMarker-user] How to modify macro paramater value and send it to another macro ?
Albert Kam
2013-07-28 16:24:39 UTC
Permalink
I would like to pass a hash to a macro, modify the hash's sequence value
within the macro, and then pass the modified hash to another macro.

I have put on a simple example to illustrate with 'non' freemarker comments
:

// try to add 'java' to plugins.tools sequence and pass it to @print
// but still the plugins passed to @print doesnt contain 'java'
<#macro printAndModify plugins>
${plugins.project} uses <@printList plugins.tools />
<#local tools=plugins.tools
tools=tools + ['java']
'plugins.tools'=tools>
<@print plugins />
</#macro>

<#macro print plugins>
${plugins.project} uses <@printList plugins.tools />
</#macro>

<#macro printList list>
<#list list as item>${item} </#list>
</#macro>

<#assign plugins={'project' : 'cool project', 'tools' : ['freemarker']}>

<@printAndModify plugins />


The output of the project is :

cool project uses freemarker

cool project uses freemarker


The expected output is :

cool project uses freemarker

cool project uses freemarker java

Is there anyway to do this in the ftl ? Thank you.
--
Do not pursue the past. Do not lose yourself in the future.
The past no longer is. The future has not yet come.
Looking deeply at life as it is in the very here and now,
the practitioner dwells in stability and freedom.
(Thich Nhat Hanh)
Daniel Dekany
2013-07-29 08:50:02 UTC
Permalink
Post by Albert Kam
I would like to pass a hash to a macro, modify the hash's sequence
value within the macro, and then pass the modified hash to another macro.
<#macro printAndModify plugins>
<#local tools=plugins.tools
tools=tools + ['java']
'plugins.tools'=tools>
</#macro>
<#local 'plugins.tools'=tools> just creates a local variable called
"plugins.tools".

You can do this like:

<#macro printAndModify plugins>
${plugins.project} uses <@printList plugins.tools />
<@print plugins + { 'tools': plugins.tools + ['java'] } />
</#macro>
--
Thanks,
Daniel Dekany
Loading...