Reputation: 1433
I need to access to next element in foreach
to compare some value.
In JSP, I added some dummy element, and loop like this;
for(int i=0; i<list.size() - 1; i++) {
MyClass element = list.get(i);
MyClass nextElement = list.get(i+1);
if(element.getSomeValue() > nextElement.getSomeValue())
doSome();
...
But in VM I can't get the element by index as far as I know. If there's some way to access to next element, please help me.
Upvotes: 0
Views: 982
Reputation: 2574
Your Velocity template (the .vm file) can access any Java object that's placed into the Context. In your backing Java class or Velocity servlet, place your list into the context:
context.put("list", myList);
Then in your Velocity template you can reference it as $list
and you can call methods on it by using $list.get(i)
, etc. Note that unlike JSP, you can't place pure Java code in a Velocity template, you must use VTL.
Upvotes: 2