Jorgito Gutierrez
Jorgito Gutierrez

Reputation: 488

Defer variable parsing in Freemarker

I need to defer the parsing of a variable that is in the top of the document so as to modify its content and then output its value it at the end of the process.

I'm using latest Freemarker version

For example:

<#global myVar="">

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

// Some other work ...

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

should output

Correct

Upvotes: 0

Views: 49

Answers (1)

ddekany
ddekany

Reputation: 31122

There's no such feature. Depending on the use case, what's sometimes working is capturing the output starting from the position where the deferred part will be, and then printing the deferred part, and then printing the captured output. For example you need to print the number of some items at the top of a section, but the items will be only known after the section was printed. Then:

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

<#macro item>
  <#assign itemCount++>
</#macro>

and then:

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

Upvotes: 1

Related Questions