Fixed #5908 -- Added {% resetcycle %} template tag.

Thanks to Simon Litchfield for the report, Uninen for the initial
patch, akaihola, jamesp, b.schube, and Florian Appoloner for
subsequent patches, tests, and documentation.
This commit is contained in:
Sergei Maertens 2016-07-03 16:19:06 +02:00 committed by Tim Graham
parent 2cfd48bccd
commit 32c02f2a0e
4 changed files with 197 additions and 0 deletions

View file

@ -185,6 +185,9 @@ call to ``{% cycle %}`` doesn't specify ``silent``::
{% cycle 'row1' 'row2' as rowcolors silent %}
{% cycle rowcolors %}
You can use the :ttag:`resetcycle` tag to make a ``{% cycle %}`` tag restart
from its first value when it's next encountered.
.. templatetag:: debug
``debug``
@ -994,6 +997,57 @@ attribute, allowing you to group on the display string rather than the
``{{ country.grouper }}`` will now display the value fields from the
``choices`` set rather than the keys.
.. templatetag:: resetcycle
``resetcycle``
--------------
.. versionadded:: 1.11
Resets a previous `cycle`_ so that it restarts from its first item at its next
encounter. Without arguments, ``{% resetcycle %}`` will reset the last
``{% cycle %}`` defined in the template.
Example usage::
{% for coach in coach_list %}
<h1>{{ coach.name }}</h1>
{% for athlete in coach.athlete_set.all %}
<p class="{% cycle 'odd' 'even' %}">{{ athlete.name }}</p>
{% endfor %}
{% resetcycle %}
{% endfor %}
This example would return this HTML::
<h1>José Mourinho</h1>
<p class="odd">Thibaut Courtois</p>
<p class="even">John Terry</p>
<p class="odd">Eden Hazard</p>
<h1>Carlo Ancelotti</h1>
<p class="odd">Manuel Neuer</p>
<p class="even">Thomas Müller</p>
Notice how the first block ends with ``class="odd"`` and the new one starts
with ``class="odd"``. Without the ``{% resetcycle %}`` tag, the second block
would start with ``class="even"``.
You can also reset named cycle tags::
{% for item in list %}
<p class="{% cycle 'odd' 'even' as stripe %} {% cycle 'major' 'minor' 'minor' 'minor' 'minor' as tick %}">
{{ item.data }}
</p>
{% ifchanged item.category %}
<h1>{{ item.category }}</h1>
{% if not forloop.first %}{% resetcycle tick %}{% endif %}
{% endifchanged %}
{% endfor %}
In this example, we have both the alternating odd/even rows and a "major" row
every fifth row. Only the five-row cycle is reset when a category changes.
.. templatetag:: spaceless
``spaceless``

View file

@ -313,6 +313,9 @@ Templates
so you can unpack the group object directly in a loop, e.g.
``{% for grouper, list in regrouped %}``.
* Added a :ttag:`resetcycle` template tag to allow resetting the sequence of
the :ttag:`cycle` template tag.
Tests
~~~~~