• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

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

 
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
 
Author
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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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.
 
If you have a bad day in October, have a slice of banana cream pie. And this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!