Michel Erard wrote:
Hello,
I'm implemented this loop with jx:

<page xmlns:jx="http://apache.org/cocoon/templates/jx/1.0";>
<jx:set var="counter" value="#{-1}"/> <jx:forEach var="item" items="${list}">
                <jx:set var="counter" value="#{1 + $counter}"/>
>                 ${item} : ${counter}<br/>
>               </jx:forEach>

this creates a new local variable which shadows the parent one and not really modifies the parent. That is why your counter only increments by 1. It is recreated for each step.

While not advised if you really need this kind of functionality (and you are not able to use the solution Felix provided) create a wrapper:

public class Incrementer() {
  private int value = -1;

  public void inc() { ++this.value; }
  public int get() { return.value; }
}

and use it like this:

<page xmlns:jx="http://apache.org/cocoon/templates/jx/1.0";>
  <jx:set var="counter" value="${Packages.com.mycompany.Incrementer()}"/>
  <jx:forEach var="item" items="${list}">
    <jx:set var="ignore" value="${counter.inc()}"/>
    ${item} : ${counter.get()}<br/>
  </jx:forEach>
</page>



--
Leszek Gawron                         http://www.mobilebox.pl/krs.html
CTO at MobileBox Ltd.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to