How can I push a variable on the value stack in freemarker
In freemarker, I'm looking to include an existing template from within another repetitively. The existing template assumes it's looking at the top of the value stack. I'm reall开发者_运维问答y looking for an 'apply' function. So I have a parent template:
<#list items as item>
<#include "/my/subtemplate.ftl"/>
</#list>
How can I make the subtemplate see item as the top item on the value stack, so that I don't need to copy it and change every reference to 'property' on the item to item.myproperty?
just confirming "stack" is a reference to the ValueStack object placed into the template's context by the Struts2 framework. It doesn't exist outside Struts2.
In FreeMarker, an included template shares the state of the including template, such as the data model and the template language variables. Unfortunately, there is no push or pop of the context like this to change the included template's context. You can use <#assign>
or <#global>
to place variables in the scope of the template, but these won't replace shared template data model.
In plain FreeMarker, I think you'd refactor the subtemplate into a macro or function and <#import>
it, passing item in as an argument.
After googling and some code reading, the following worked for me:
<#list items as item>
<#assign dummy=stack.push(item)/>
<#include "/my/subtemplate.ftl"/>
<#assign dummy=stack.pop()/>
</#list>
精彩评论