Joe
Joe

Reputation: 237

Limit twig loop

How can I make the following iteration list 5 items max only?

<ul class="list-unstyled childs_2">  
{% set wi = 0 %}
{% for wi in wi..category.children[i]['children_level2']|length %}
<li><a href="{{ category.children[i]['children_level2'][wi]['href'] }}">
{{ category.children[i]['children_level2'][wi]['name'] }}</a>
</li>  
{% endfor %}        
</ul>

Upvotes: 3

Views: 16794

Answers (1)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

I think iterating over a subset may work for what you're doing here. With that approach, the wi variable shouldn't be necessary, unless you're using it for something else as well.

<ul class="list-unstyled childs_2">  
{% for child in category.children[i]['children_level2']|slice(0, 5) %}
    <li>
        <a href="{{ child['href'] }}">{{ child['name'] }}</a>
    </li>  
{% endfor %}        
</ul>

Upvotes: 14

Related Questions