【问题标题】:Defer variable parsing in Freemarker在 Freemarker 中延迟变量解析
【发布时间】:2019-08-29 17:57:41
【问题描述】:

我需要延迟解析文档顶部的变量,以便修改其内容,然后在过程结束时输出其值。

我正在使用最新的 Freemarker 版本

例如:

<#global myVar="">

// Output here
${myVar} <<<<< DEFER HERE!

// Some other work ...

// Change its value
<#global myVar="Correct">

应该输出

Correct

【问题讨论】:

    标签: freemarker


    【解决方案1】:

    没有这样的功能。根据用例,有时工作是从延迟部分所在的位置开始捕获输出,然后打印延迟部分,然后打印捕获的输出。例如,您需要在一个部分的顶部打印某些项目的数量,但这些项目只有在该部分打印后才能知道。那么:

    <#macro sectionWithCounterOnTop>
      <#assign itemCount = 0>
      <#local sectionContent><#nested></#local>
      Items: ${itemCount}
      ${sectionContent}
    </#macro>
    
    <#macro item>
      <#assign itemCount++>
    </#macro>
    

    然后:

    <@sectionWithCounterOnTop>
      ...
      <@item />
      ...
    </@sectionWithCounterOnTop>
    

    【讨论】:

    • 我会试试的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多