For loops
Das Objekt forloop enthält die Attribute seiner übergeordneten for-Schleife.
Inhalt
- forloop.first
- forloop.index
- forloop.index0
- forloop.last
- forloop.length
- forloop.rindex
- forloop.rindex0
forloop.first
Gibt true zurück, wenn es die erste Iteration der for-Schleife ist. Gibt false zurück, wenn es nicht die erste Iteration ist.
{% for product in Sammlungen.frontpage.products %}
{% if forloop.first == true %}
Das erste Mal durch!
{% else %}
Nicht das erste Mal.
{% endif %}
{% endfor %}
Das erste Mal durch! Nicht das erste Mal. Nicht das erste Mal. Nicht das erste Mal. Nicht das erste Mal.
forloop.index
Gibt den aktuellen Index der for-Schleife zurück, beginnend bei 1.
{% for product in Sammlungen.frontpage.products %}
{{ forloop.index }}
{% else %}
// no products in your frontpage collection
{% endfor %}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
forloop.index0
Gibt den aktuellen Index der for-Schleife zurück, beginnend bei 0.
{% for product in Sammlungen.frontpage.products %}
{{ forloop.index0 }}
{% endfor %}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
forloop.last
Gibt true zurück, wenn es die letzte Iteration der for-Schleife ist. Gibt false zurück, wenn es nicht die letzte Iteration ist.
{% for product in Sammlungen.frontpage.products %}
{% if forloop.last == true %}
Dies ist die letzte Iteration!
{% else %}
Weitermachen...
{% endif %}
{% endfor %}
Weitermachen... Weitermachen... Weitermachen... Weitermachen... Weitermachen... Dies ist die letzte Iteration!
forloop.length
Gibt die Anzahl der Iterationen der Schleife zurück.
<!-- if Sammlungen.frontpage.products contains 4 products -->
{% for product in Sammlungen.frontpage.products %}
{% if forloop.first %}
<p>This collection has {{ forloop.length }} products:</p>
{% endif %}
<p>{{ product.title }}</p>
{% endfor %}
Diese Kollektion umfasst 4 Produkte: Birne Mandarine Nektarine Kirche
forloop.rindex
Gibt forloop. index in umgekehrter Reihenfolge zurück.
{% for product in Sammlungen.frontpage.products %}
{{ forloop.rindex }}
{% endfor %}
16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
forloop.rindex0
Gibt forloop. index0 in umgekehrter Reihenfolge zurück.
{% for product in Sammlungen.frontpage.products %}
{{ forloop.rindex0 }}
{% endfor %}
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0