Silverstripe检查是否在循环中的第一次迭代

Boj*_*jan 2 silverstripe

如何检查循环是否在第一次迭代?

我想将一个类添加到列表项但仅添加到第一个列表,而不是所有列表.

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

有没有办法实现这个目标?我不想通过javascript这样做.

Zau*_*sch 10

就在这里.

请参阅模板上的SilverStripe文档,位置指示器部分

<% 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 %>
Run Code Online (Sandbox Code Playgroud)

  • 我想如果你想检查一个特定的位置编号,你可以做<%if $ Pos = 3%> (3认同)