This example illustrate how to use a nested “for” in twig also the use of a loop instruction:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<ul> {% for child in childs %} <li> <span>{{child.name}}</span> <ul style="list-style: square;padding-left: 20px;"> {% for item in items[child.id] %} {% if loop.index < 4 %} <li><span>{{ item.title }}</span></li> {% else %} <li style="display:none;"><span>{{ item.title }}</span></li> {% endif %} {% endfor %} </ul> </li> {% endfor %} </ul> |
Inside of a for loop block you can access some special variables: Variable Description loop.index The current iteration of the loop. (1 indexed) loop.index0 The current iteration of the loop. (0 indexed) loop.revindex The number of iterations from […]