문제

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.

도움이 되었습니까?

해결책

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 %>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top