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.

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.x
    • Fix Version/s: 2.0, 2.x, 3.0
    • Component/s: GenericTools
    • Labels:
      None
    • Environment:
      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

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

              Dates

              • Created:
                Updated:
                Resolved: