Dynamic list
Kirill <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Hi All,
I've been fighting with this for a while and now out looking for help.
I am creating a generic template, used to work with XML data model.
Here is how the template looks right now:
<#ftl>
<#-- this will be a Freemarker Template -->
<#noparse><#ftl ns_prefixes={"D":"http://our.site.com/ns"}></#noparse>
<#list .data_model?keys as key>
<#if .data_model[key]?is_node>
<#recurse .data_model[key]>
</#if>
</#list>
<#macro @element>
<${.node?node_name} <@attributes/><#if
!.node?children?has_content>/</#if>>
<#if .node?children?size = 1>
${r"${"}${varPath}.<@nodePath/>${.node?node_name}${r"}"}
</#if>
<#if .node?children?size > 0>
<#recurse>
</#if>
<#if .node?children?has_content>
</${.node?node_name}>
</#if>
</#macro>
<#macro @text>
</#macro>
<#macro attributes>
<#if .node.@@?has_content>
<@compress single_line=true>
<#list .node.@@ as attribute>
${attribute?node_name}=${r"${"}${varPath}.<@nodePath/>${.node?node_name}.@${attribute?node_name}${r"}"}
</#list>
</@compress>
</#if>
</#macro>
<#macro nodePath>
<@compress single_line=true>
<#list .node?ancestors?reverse as node>
<#if node?node_type = 'element'>
${node?node_name}.<#t>
</#if>
</#list>
</@compress>
</#macro>
And here is where I'm stuck. Sometimes, there are multiple @element's
out there, for example, XML looks like this:
<root>
<parent>
<child/>
<child/>
<child/>
</parent>
</root>
With the current template, in the output I'm getting all of the
<child> nodes, and all of them have same variable names, of course.
What I need to do - is create a list of those nodes, so the result is
smth like: <#list nodes as node>${node?node_name}</#list>
How can I create such a list, without breaking the <#macro @element> ?
Or, if this needs to be re-done, what's the best possible way here?
Thanks a bunch for all the help.
- Kirill
------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H