0
votes

In one of my FreeMarker templates I have a pair of macros which look something like this

<#macro RepeatTwice><#nested 1><#nested 2></#macro>

<#macro Destinations>
<#assign DestinationList = []>
<@RepeatTwice ; Index>
<#assign City == some XPath expression dependant on the index>
<#if City == something>
<#assign DestinationList = DestinationList + ["some string"]>
<#elseif City == something else>
<#assign DestinationList = DestinationList + ["some string"]>
</#if>
</@>
</#macro>

So now, my destination list contains 2 values. This works in that if I insert ${DestinationList[0]} or ${DestinationList[1]} before </#macro> I get the expected output.

My question is --- in a separate template I would like to capture the whole of the list from this macro so that I can access its elements as and when I need them.

For this I need the macro to return the whole list, not just an element from it.

But inserting ${DestinationList} or just DestinationList returns an error.

I have tried doing things like ${DestinationList[0,1]} and the like but nothing seems to work.

Is this possible?

Thanks,

Basil

1

1 Answers

0
votes

Macros (and directives) are to print to the output (and to have whatever other side-effects), not to calculate values. You need to define a function here. That you do with #function instead of #macro, and then you can use #return to return whatever value (like a list).

Also note that ${expression} always converts the value to string, that's why you get the error.