Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SilverStripe Children Of, loop only 3 times

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?

like image 699
Bagzli Avatar asked Dec 07 '25 16:12

Bagzli


1 Answers

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 %>
like image 147
Shane Garelja Avatar answered Dec 09 '25 18:12

Shane Garelja



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!