wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Newbie Watch "Newbie New topic
Author

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

<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


SCJA 1.0
David Newton
Author
Rancher

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é
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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
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.
 
 
subject: Newbie 'FreeMarker' template Question (usage of #list)
 
Similar Threads
jsp table
Struts1: evaluating two conditions
Need Help with Jquery Selector logic
Sending Email from jsp page
JSP Design - Basics