문제

I'm trying to make the loop run only 3 times, no matter how many it has.

<% loop $ChildrenOf(Sponsors) %>
        <li>
            <a href="$TargetURL" target="_blank">
                <img src="$Logo.Link" alt="image" />
            </a>
        </li>
    <% end_loop %>

What I thought about doing is declaring a counter variable and if it hits 3 call break, however when I do this:

<% $counter = 0 %>

the page breaks, it doesn't throw any errors but any line of code after that doesn't get rendered.

How would I make that loop break after it runs 3 times?

도움이 되었습니까?

해결책

This is covered in the SilverStripe Templates documentation. This should work:

<% loop $ChildrenOf(Sponsors).Limit(3) %>
    <li>
        <a href="$TargetURL" target="_blank">
            <img src="$Logo.Link" alt="image" />
        </a>
    </li>
<% end_loop %>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top