Re: hash & sequence

Jonathan Revusky <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
On Thu, Feb 12, 2009 at 9:30 PM, dirk ooms <[email protected]> wrote:
> Hello,
>
> I have data that is stored in a LinkedHashMap, so it has both a hash and
> a sequence nature. I would like to address this data in a Freemarker
> template in both its natures. In an attempt to achieve this, I created
> my own class that extends LinkedHashMap and that provides the sequence
> interface methods (get(int) and size()). But when I try to use it in the
> <#list> construct Freemarker says:
>
> Expected collection or sequence. a.b.c evaluated instead to
> freemarker.template.SimpleHash
>
> So it does not recognize it as a sequence. Is there a way I can use my
> LinkedHashMap in a <#list>?

There are two potential answers:

First, if you want fine control over the access methods (i.e.
implement them yourself) you can explicitly implement the interfaces
TemplateHashModelEx and TemplateSequenceModel in your subclass.

Second (and this is probably the better first-pass answer)  if you
don't want to do that, and just use the automatic BeansWrapping
machinery, it should work, you just write:

<#list linkedHashMap?keys as key>....</#list>

or

<#list linkedHashMap?values as value>...</#list>

depending on whether you want to iterate over the keys or values of
course. And the order (unlike with a regular HashMap) should be
maintained, I think.

JR

>
> Thanks,
>
> dirk
>
>
> ------------------------------------------------------------------------------
> _______________________________________________
> 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
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.