Increment integer on loop

matthew.wren's Avatar

matthew.wren

24 Aug, 2016 06:39 PM

I know this should be a pretty easy task but I can't seem increase an integer value by 1 on each loop. I'm pretty sure I'm doing something wrong but here's my loop:

#foreach($video in $_XPathTool.selectNodes($contentRoot, "/system-data-structure/featured-videos/video"))
     #set ($num = $_MathTool.toNumber(1))
          <div role="tabpanel" class="tab-pane active" id="youtube-${num}">
          </div>
     #set ($num = $_MathTool.add(1))    
#end
  1. 1 Posted by Ryan Griffith on 24 Aug, 2016 07:20 PM

    Ryan Griffith's Avatar

    Hi Matthew,

    Loops actually have an existing iterator that can be accessed using $foreach.index. For additional information about available properties within loops, see this page.

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by matthew.wren on 24 Aug, 2016 07:26 PM

    matthew.wren's Avatar

    oh that's amazing! is there anyway to make the iterator start at 1 rather than 0?

  3. 3 Posted by Ryan Griffith on 24 Aug, 2016 08:43 PM

    Ryan Griffith's Avatar

    Hi Matthew,

    You would have to implement something similar to what you were doing and add one to the iterator:

    #set ($videos = $_XPathTool.selectNodes($contentRoot, "/system-data-structure/featured-videos/video"))
    #foreach($video in $videos)
              <div role="tabpanel" class="tab-pane active" id="youtube-${_MathTool.add($foreach.index, 1)}">
              </div>
    #end
    

    Please let me know if you have any questions.

    Thanks!

Comments are closed, but you can start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac