Increment integer on loop

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. 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. oh that's amazing! is there anyway to make the iterator start at 1 rather than 0?

  3. 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!

Discussions are closed to public comments.
If you need help with Cascade CMS please 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