List 自由标记传输列表
在我的一个FreeMarker模板中,我有一对宏,看起来像这样List 自由标记传输列表,list,indexing,freemarker,List,Indexing,Freemarker,在我的一个FreeMarker模板中,我有一对宏,看起来像这样 <#macro RepeatTwice><#nested 1><#nested 2></#macro> <#macro Destinations> <#assign DestinationList = []> <@RepeatTwice ; Index> <#assign City == some XPath expression depen
<#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>
现在,我的目的地列表包含2个值。这是因为如果我在
之前插入${DestinationList[0]}
或${DestinationList[1]}
,我将获得预期的输出
我的问题是---在一个单独的模板中,我想从这个宏中捕获整个列表,这样我就可以在需要时访问它的元素
为此,我需要宏返回整个列表,而不仅仅是其中的一个元素
但是插入${DestinationList}
或仅插入DestinationList会返回错误
我试过做一些类似于${DestinationList[0,1]}
之类的事情,但似乎没有任何效果
这可能吗
谢谢
Basil宏(和指令)将打印到输出(并产生任何其他副作用),而不是计算值。您需要在这里定义一个函数。使用#函数
而不是#宏
,然后可以使用#return
返回任何值(如列表)
还要注意,${expression}
总是将值转换为字符串,这就是为什么会出现错误