+1
W trakcie analizy

Limit the count on a loop without creating a variable

Stephen Gilbert 11 lat temu 2

A way to output a select amount of items in a loop without having to create an additional variable.
{loop items="#categories" value="category" limit="3"}
{#category.name}
{/loop}




This would be useful on blogs when only wanting to list one category, or gallery sites where the first few images have large images.

An alternative I suggested to Stephen which would achieve the same effect is to use {break} (or something along those lines) to break a foreach loop.

Ex.:

{loop items="#items" value="item" key="key"}
{if #key = 2} {break} {/if}
{/loop}



I think that both options could actually work well and would have their own use cases.

Having the "limit" parameter would work well when you know exactly how many items you want.

The {break} tag would work well in cases where some other logic is used to exit the loop.