Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to get string list values in velocity template? foreach loop for 3 lists

Hi All,

I have 3 String lists(lets say ListA, ListB and ListC) in my java class which maps to velocity template. I need to display the values of the list in a table.

As velocity allows only foreach loop i can get values of only one list. is there a way to get values from all three lists?

If not in foreach loop, how to access the list values through its index?

 

Any inputs will be much appreciated.

Thanks

2 answers

1 accepted

1 vote
Answer accepted

Hi, 

with a little more investigation i found the answer.

from java class, lets say we have following code with populateVelocityParams method

params.put("listA",ListA);
//Map the below java utils List class 
params.put("list",List.class);

 

then in velocity template, 

just call 

$listA.get(index);
//"index" here is the list integer index position in ListA

 

Thanks,

Sharath TS

Velocity does not iterate Lists, as far as I can tell.

Velocity documentation says that it iterates over Vector, Hashtable, or an array (not a List).

In my experience, that is true.  

https://velocity.apache.org/engine/1.7/user-guide.html#foreach-loop 

https://velocity.apache.org/engine/2.1/user-guide.html#foreach-loop 

I tried to make my velocity template iterate over a List and it did nothing.  I converted my List to an array and it worked fine.

I know that this thread is really old but other, newer, threads point to it (that's how I found it) and this thread made me think I could iterate over a List.  I could not.

I am developing a Confluence plugin, don't know if my pom.xml versions are out of date and some new Velocity engine or Atlassian ingredient changes things but beware:  in my case, Velocity does NOT iterate over a List.  It will, however iterate just fine over an array.

 

My template:

#foreach($result in $results)
<tr><td>${result}</tr></td>
#end

 

My getter in Java:

public String[] getResults() {
return results;
}

 

For my first try, using List<String> for my results the template rendered nothing for my List.  I went with an array and it works.  Decided not to mess with Vector for various reasons.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Agile

Join us LIVE: Atlassian & Experts Talk Research & Insights @ Scale

Hello all! What have you learned from your customers lately? Our live-streamed series continues by exploring CX, UX, and the power of research & insights at scale with Leisa Reichelt, Head of R...

251 views 3 6
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you