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 %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With