We have an array of objects that need to be displayed.

{% for item in items %} <li></li> {% endfor %} 

Also the item can be active

 {% for item in items %} <li class="{% if smth == smth2 %}active{% endif %}"></li> {% endfor %} 

Since the element can be many, and the block is limited in height, an internal scroll is required, but the active element can be lost in this footcloth, so you need to display it as the first element

 {% for item in items %} {% if smth == smth2 %} {% if loop.index == 1 %} <li class="active"></li> {% endif %} {% else %} <li></li> {% endif %} {% endfor %} 

This is all I have come to, but it does not work.

    1 answer 1

    If you want to do this in the template (without using sorting in the controller), then the easiest way is to go through the items array twice:

     <!-- сначала выводим только активные --> {% for item in items %} {% if smth == smth2 %} <li class="active"></li> {% endif %} {% endfor %} <!-- а потом всё остальное --> {% for item in items %} {% if smth != smth2 %} <li></li> {% endif %} {% endfor %}