Re: Splitting a list and adding elements.
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Usually the FreeMarker templates should concentrate on presentation
(displaying data as nice HTML, usually), not filter and sorting.
Filter and sorting should be done when you build the data-model (In
Java, Groovy, Scala, whatever). But if you can't/shouldn't do it that
way... core FreeMarker doesn't support this (it's not designed for
this), but you can write TemplateMethodModelEx-s (and
TemplateDirectiveModel-s) or enrich the data-model with special
wrappers (as an example of this there's the XML wrapper, where you can
issue XPath expression to filter). Writing special wrappers is maybe
the nicest hardest to implement.
Tuesday, March 8, 2011, 6:57:20 PM, Paulo Ferreira wrote:
> Hello!
>
> I'd like to know if there's a way on Freemarker to divide/split a list.
>
> I have my list full of news, but I'd like to split them in two other lists.
> One for 'published' and the other for 'not published'. What I've tried so
> far:
>
> [#assign myNodeList = myContent?children?sort_by("news_date")]
> [#assign myNodeListPublished = [] ]
> [#assign myNodeListNotPublished = [] ]
>
> [#list myNodeList as myNode]
> [#if myNode.published == "true"]
> //add myNode to myNodeListPublished//
> [#else]
> /add myNode to myNodeListNotPublished //
> [/#if]
> [/#list]
>
> Or is there a way that I could assign directly using a filter, just like
> this idea:
>
> [#assign myNodeList = myContent?children?sort_by("news_date")]
> [#assign myNodeListPublished = myNodeList.where("published" == true) ]
>
> Any ideas ?
>
> Thanks,
>
> Paulo.
--
Best regards,
Daniel Dekany
------------------------------------------------------------------------------
Colocation vs. Managed Hosting
A question and answer guide to determining the best fit
for your organization - today and in the future.
http://p.sf.net/sfu/internap-sfd2d