【问题标题】:Freemarker macros with few nested elements嵌套元素很少的 Freemarker 宏
【发布时间】:2012-10-29 11:25:21
【问题描述】:

如何在宏中包含几个不同的 #nested 元素?

【问题讨论】:

    标签: macros freemarker


    【解决方案1】:

    宏中不能有不同的#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.
    

    【讨论】:

    • 只要你没有打开auto-escaping,这个解决方案就可以很好地工作,这实际上是个好主意。如果footer 预计包含标记,我通常会使用${footer?no_esc} 来禁用转义。
    猜你喜欢
    • 2013-12-03
    • 2022-01-14
    • 2012-10-19
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 2013-05-28
    • 1970-01-01
    相关资源
    最近更新 更多