【发布时间】:2012-10-29 11:25:21
【问题描述】:
如何在宏中包含几个不同的 #nested 元素?
【问题讨论】:
标签: macros freemarker
如何在宏中包含几个不同的 #nested 元素?
【问题讨论】:
标签: macros freemarker
宏中不能有不同的#nested元素,每次使用都会输出相同的文本。
如果您的目标是在宏中包含多个变量部分,则可以使用 #assign 元素。
页面示例#macro允许定义正文、页眉和页脚内容:
<#macro pageTemplate header="" footer="">
${header}
<#nested >
${footer}
</#macro>
然后,您可以使用 #assign 元素定义每个部分(当然,拥有多个命名的 #nested 元素会更好)。
<#assign headerContent>
This is the header.
</#assign>
<#assign footerContent>
This is the footer.
</#assign>
<@pageTemplate header=headerContent footer=footerContent>
This is the nested content.
</@pageTemplate>
结果输出将是:
This is the header.
This is the nested content.
This is the footer.
【讨论】:
footer 预计包含标记,我通常会使用${footer?no_esc} 来禁用转义。