Velocity
  1. Velocity
  2. VELOCITY-657

$velocityHasNext not working properly (returns true even if iterator does not have next value)

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.6
    • Fix Version/s: 1.6.2, 1.7
    • Component/s: Engine
    • Labels:
      None

      Description

      Using the new "velocityHasNext" feature in loops does not work as expected.

      When using the following excerpt:

      #foreach ($value in $element.values)
      $value
      #if( $velocityHasNext )
      SEPARATOR
      #end
      #end

      and having $element.values as e.g. "test1", "test2", "test3"

      I get

      test1 SEPARATOR test2 SEPARATOR test3 SEPARATOR

      but I would expect

      test1 SEPARATOR test2 SEPARATOR test3

      When looking into the source code, I see the following in org.apache.velocity.runtime.directive.Foreach.render():

      while (!maxNbrLoopsExceeded && i.hasNext())
      {
      // TODO: JDK 1.5+ -> Integer.valueOf()
      put(context, counterName , new Integer(counter));
      put(context, hasNextName, Boolean.valueOf(i.hasNext()));
      Object value = i.next();
      put(context, elementKey, value);
      ....
      }

      Isn't this the wrong order of instructions?

      I would expect:

      Object value = i.next();
      put(context, hasNextName, Boolean.valueOf(i.hasNext()));

      So that the $velocityHasNext variable will be filled with the "hasNext" of the follow-up iteration and not with the "hasNext" of the current iteration (which will always be true, otherwise the loop has finished before).

        Issue Links

          Activity

          Dominik Marks created issue -
          Jarkko Viinamäki made changes -
          Field Original Value New Value
          Attachment velocity-657-testcase.patch [ 12396698 ]
          Jarkko Viinamäki made changes -
          Link This issue relates to VELOCITY-658 [ VELOCITY-658 ]
          Nathan Bubna made changes -
          Status Open [ 1 ] Resolved [ 5 ]
          Resolution Fixed [ 1 ]
          Fix Version/s 1.6.2 [ 12313574 ]
          Fix Version/s 1.7 [ 12313453 ]
          mjparme made changes -
          Comment [ I am using velocity 1.6.2 and this is still occuring. Using $velocityHasNext does not work in nested loops.

          For example:

          I have: #foreach(${column} in ${columns})${column.columnName}#if(${velocityHasNext}),#end#end

          However, if I have two columns named "foo" and "bar" this is resulting in "foobar". If I change the condition in the if statment to something cheesy like (3 > 2) then the expected results of "foo,bar" is generated.

          So the $velocityHasNext is still not working in velocity 1.6.2.

          I have this property in my velocity.properties:

          directive.foreach.iterator.name = velocityHasNext
          ]
          Mark Thomas made changes -
          Workflow jira [ 12449039 ] Default workflow, editable Closed status [ 12551974 ]
          Mark Thomas made changes -
          Workflow Default workflow, editable Closed status [ 12551974 ] jira [ 12552352 ]

            People

            • Assignee:
              Unassigned
              Reporter:
              Dominik Marks
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development