Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Chintan B Shah
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

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

<tr>
<td>A</td><td>B</td><td>C</td>
</tr>

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>
<tr>
<td>${a}</td><td>${b}</td><td>${c}</td>
</tr>
<#list>
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.

Regards,
Chintan
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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é
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chintan B Shah
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris/David,

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.

Regards,
Chintan.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic