Re: Dynamic list

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Wednesday, February 25, 2009, 2:55:12 AM, Kirill wrote:

> 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 &gt; 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>

(I don't think you need this @compress, since you are already using
<#t>.)

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

I'm not sure what you want to achieve... demonstrate the problem with
a more bare-bone example, because I don't think others will follow
either.

> 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
> _______________________________________________
> FreeMarker-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.