Uploaded image for project: 'Velocity Tools'
  1. Velocity Tools
  2. VELTOOLS-124

LoopTool fails to retrieve $loop.index, $loop.first etc for the last element in a collection.

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.x
    • 2.0, 2.x, 3.0
    • GenericTools
    • None
    • velocity-tools-2.0-beta4 with velocity-1.6.2

    Description

      $loop.index, $loop.first and other LoopTool methods (that depend on iterator) return null for the last element in a collection.

      Try to run:
      #set( $list = [1..3] )
      #foreach($i in $loop.watch($list))
      i[${loop.index}]=$i;
      #end

      The result is:
      i[0]=1; i[1]=2; i[${loop.index}]=3;

      I tried to investigate it a little, and seems like the problem is inside "cacheNext(boolean popWhenDone)".

      First it checks if (!iterator.hasNext()) and if not then removes this iterator from the stack. But later in this method it retrieves next element from a collection "this.next = iterator.next();". Seems like it causes removing iterator too early, when calling ${loop.index} on the last element the iterator is already removed.

      Thanks.

      Attachments

        Activity

          People

            Unassigned Unassigned
            serg472 Sergiy Kovalchuk
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: