Bagzli
Bagzli

Reputation: 6569

Silverstripe check if on first iteration in loop

How can I check if the loop is on its first iteration?

I want to add a class to a list item but only to the first list, not all of them.

<% loop $LatestNews(3) %>
    <% if $this->iteratorPos == 0 %>
    <li class="left-arrow highlight">$Title</li>
    <% else %>
        <li>$Title</li>
    <% end_if %>
<% end_loop %>

Is there a way to achieve this? I don't want to do this through javascript.

Upvotes: 2

Views: 3881

Answers (1)

Zauberfisch
Zauberfisch

Reputation: 4015

yes, there is.

please see the SilverStripe documentation on Templates, section Position Indicators

<% loop $SomeList %>
    <% if $First %>
        this is the first item<br>
    <% else_if $Last %>
        this is the last item<br>
    <% else %>
        this is item number $Pos<br>
    <% end_if %>
<% end_loop %>

Upvotes: 11

Related Questions