tags:

views:

27

answers:

1

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 really 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?

+1  A: 

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>
Peter Hart
I get "expression stack is undefined on line ..." Should you modify the template in some way to get access to the stack variable?
Pieter Breed
Hmm, this worked for me within Struts2 (implementing a custom tag). It might be that Struts2 does something to make the stack available like this.
Peter Hart