Jump to:

3401 Posts in 979 Topics by 855 members

Template Questions

SilverStripe Forums » Template Questions » Zero-based Loop counter

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 280 Views
  • Spaghetti
    Avatar
    Community Member
    31 Posts

    Zero-based Loop counter Link to this post

    This sounds silly and basic but I've been struggling with it for a while so, I need help...

    All I'm trying to do is output some zero-based index values for some tags I need for my carousel object. These index pictures. I've got everything working except I can't find a simple way to output a zero-based value for the $Pos bit in the code below:

    <ol id="carousel-controls" class="carousel-indicators">
    <% loop $Photos %>
    <li data-target="#myCarousel" data-slide-to="$Pos-1"></li>
    <% end_loop %>
    </ol>

    This will (obviously) output data-slide-to values that look like 1-1, 2-1, 3-1, and not 0 1 2, like I want. Any ideas how to fix this?

    I tried using {$Pos-1} but that seemed to cause a syntax error. I guess I misunderstood the docs there.

    I don't think I can use a function because the value would be cached... wouldn't it?

    I'm confused. Help please?

  • Spaghetti
    Avatar
    Community Member
    31 Posts

    Re: Zero-based Loop counter Link to this post

    Apparently you can just pass (0) as a parameter to $Pos and it's then zero-based. I couldn't find where Pos was defined in the documentation though or any mention of this - found the answer in a post elsewhere.

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: Zero-based Loop counter Link to this post

    FYI: http://api.silverstripe.org/3.1/class-SSViewer_BasicIteratorSupport.html#_Pos

    You can just search for nearly all the template parameters like $Pos in the API docs

    280 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.