Re: Dynamic list
Kirill <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Hi Daniel,
Here is the scenario. Let's say I have this XML as an input (data-
model):
<root>
<parent1>
<child1>text</child1>
<child1>text</child1>
</parent1>
<parent2>text</parent2>
<parent2>text</parent2>
<parent2>text</parent2>
</root>
Based on the template I use and more specifically, this part
(simplified):
<#macro @element>
<#-- this emits an opening tag -->
<${.node?node_name} <#if!.node?children?has_content>/</#if>>
<#-- this emits variable name, with proper formatting -->
<#if .node?children?size = 1>
${r"${"}${.node?node_name}${r"}"}
</#if>
<#if .node?children?size > 0>
<#recurse>
</#if>
<#-- this emits closing tag -->
<#if .node?children?has_content>
</${.node?node_name}>
</#if>
</#macro>
The output will be this:
<root>
<parent1>
<child1>${child1}</child1>
<child1>${child1}</child1>
</parent1>
<parent2>${child2}</parent2>
<parent2>${child2}</parent2>
<parent2>${child2}</parent2>
</root>
What I'd like to achieve is this:
<root>
<parent1>
<#list child1 as childItem1>
<child1>${childItem1}</child1>
</#list>
</parent1>
<#list parent2 as parentItem2>
<parent2>${parentItem2}
</#list>
</root>
So, basically, instead of outputting tags with the same name, create a
list of such items.
In this particular case, Freemarker has to detect if there is more
than one element with a same name,
and if there is - create a list of those elements.
I've managed to find a workaround, with a simple test variable, and by
appending [0], [1], etc to variable names.
But <#list> should look a lot better though, and easier to understand
for template users.
Thanks again for your input.
Cheers,
Kirill
On Feb 25, 2009, at 2:38 AM, Daniel Dekany wrote:
> 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 > 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
> _______________________________________________
> FreeMarker-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
------------------------------------------------------------------------------
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