Meaningless Drivel is fun!
The moose likes Other Open Source Projects and the fly likes Newbie 'FreeMarker' template Question (usage of #list) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Newbie Watch "Newbie New topic

Newbie 'FreeMarker' template Question (usage of #list)

Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Hi all,

I have been from last 2 days and I need some help writing up template for a scenario like below


where A, B,C represent data from List Data Structure in Java.

Now, I was looking at #list directive to do this...but I could not find anything where I could do multiple initializations within the same list directive.

I am trying to achieve something like this:

<#list A as a, B as b, C as c>
so that I can use values from all 3 lists at same time.

How can we do this with FreeMarker?

Also, if somebody knows how we can just specify 1 List in #list directive and get data for remaining lists, that would work too.

Any help is appreciated.

Thanks in advance.


SCJA 1.0
David Newton

Joined: Sep 29, 2008
Posts: 12617

I'm not entirely sure what you're trying to do. Are you trying to iterate over multiple lists at the same time? I'm pretty sure you can't do that (but could be wrong). It'd be better to build a more reasonable data structure before you get to the template.
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

No, it's not possible to loop through multiple lists like that. But, if these lists are arrays or implement the List interface, and have the same size, you can use the index of the loop : (I think, needs to be checked...)

But as David said, a better data structure should avoid that crap

[My Blog]
All roads lead to JavaRanch
Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83

Thanks for your assistance.

You are right.

I ended up using combination of HashMaps and List and code suggested by Chris.

Basically, I am iterating through HashMaps which include Lists. Then, I am "templating" those HashMaps using #list directive.

Yes, all the maps will be of same size.

Pseudo-code (Java) .

Pseudo-code(in Template)

I know its a little vulgar code but it works.

I tried to do it just with ArrayLists only(without using Hashmaps) and it did not work.

Here's error message I was receiving using only Lists.
Expected hash. notificationIncident evaluated instead to freemarker.template.SimpleSequence on line 89, column 29 in Report.xhtml

Thanks once again for for clue.

I agree. Here's the link:
subject: Newbie 'FreeMarker' template Question (usage of #list)
It's not a secret anymore!