Opened 17 years ago

Closed 8 years ago

#5908 closed New feature (fixed)

Add a resetcycle template tag

Reported by: Simon Litchfield <simon@…> Owned by: Sergei Maertens
Component: Template system Version: dev
Severity: Normal Keywords: cycle templatetag tag counter for loop reset
Cc: simon@…, ville@…, girzel@…, imbaczek@…, apacheco.uy@…, hr.bjarni+django@…, vbmendes@…, joe@…, Chris Chambers, jeffschroeder@…, isaacsutherland@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Under the following example, I would expect the cycle tag's internal counter to be reset after leaving the inner loop. That way it'd be 'nesting safe' :-) Thoughts, anyone?

{% for group in grouped %}
  {% for item in group.list %}
   ... {% cycle row1,row2,row3 %} ...
  {% endfor %}
{% endfor %}

Attachments (7)

cycle.py (3.5 KB ) - added by Simon Litchfield <simon@…> 17 years ago.
New safe_cycle tag which respects for loop nesting
resetcycle.diff (2.4 KB ) - added by Ville Säävuori 17 years ago.
First stab at resetcycle tag
5908_resetcycle.2.diff (7.0 KB ) - added by Antti Kaihola 16 years ago.
Patch: add the {% resetcycle %} tag, with tests and documentation
5908-resetcycle.3.diff (7.3 KB ) - added by jamesp 13 years ago.
Updated patch to current trunk; added TemplateSyntaxError tests
5908-resetcycle.4.diff (7.7 KB ) - added by b.schube@… 12 years ago.
Updated patch to current trunk
5908-resetcycle.5.diff (7.6 KB ) - added by b.schube@… 12 years ago.
small correction
t5908.diff (7.9 KB ) - added by Florian Apolloner 12 years ago.

Download all attachments as: .zip

Change History (35)

comment:1 by anonymous, 17 years ago

Cc: simon@… added

comment:2 by anonymous, 17 years ago

Triage Stage: UnreviewedDesign decision needed

by Simon Litchfield <simon@…>, 17 years ago

Attachment: cycle.py added

New safe_cycle tag which respects for loop nesting

comment:3 by anonymous, 17 years ago

Has patch: set

comment:4 by Malcolm Tredinnick, 17 years ago

Triage Stage: Design decision neededAccepted

The current behaviour of the cycle tag is very useful, so we don't want to change that. The scope of a cycle tag is everywhere after it is defined for just this reason. Adding another cycle tag isn't a neat solution, either.

The solution here is to add a "resetcycle" tag. Given no arguments, it reset the most recent looped cycle tag. Given an argument, it resets the cycle tag with that name.

comment:5 by Ville Säävuori, 17 years ago

Cc: ville@… added

by Ville Säävuori, 17 years ago

Attachment: resetcycle.diff added

First stab at resetcycle tag

comment:6 by Ville Säävuori, 17 years ago

I made a patch based on Malcolms last comment. This works for me but I'm not quite sure if it is fully valid solution. If someone could take a look and comment this, it would be appreciated.

comment:7 by anonymous, 17 years ago

Cc: girzel@… added

comment:8 by imbaczek@…, 17 years ago

I've just ran into this very problem, this needs a solution before 1.0 IMHO.

comment:9 by anonymous, 17 years ago

Cc: imbaczek@… added

comment:10 by anibal, 16 years ago

Cc: apacheco.uy@… added

Yes, I think this will be useful too. Any news?

comment:11 by Antti Kaihola, 16 years ago

Replying to Uninen:

I made a patch based on Malcolms last comment. This works for me but I'm not quite sure if it is fully valid solution. If someone could take a look and comment this, it would be appreciated.

I see a couple of problems (looking at the code, didn't actually test it):

  • reserves the {{ resetcycle }} context variable; any templates which happen to use it will be broken
  • can't reset multiple simultaneous cycles (will forget previous reset)
  • no tests
  • no documentation

I'll try to think about this a bit.

by Antti Kaihola, 16 years ago

Attachment: 5908_resetcycle.2.diff added

Patch: add the {% resetcycle %} tag, with tests and documentation

comment:12 by Antti Kaihola, 16 years ago

The above patch adds a {% resetcycle %} tag which does not pollute the context. It should be able to reset the last unnamed and multiple named {% cycle %} tags.

The patch also lives in a GitHub branch.

comment:13 by hejsan, 15 years ago

Cc: hr.bjarni+django@… added

comment:14 by vbmendes, 15 years ago

Cc: vbmendes@… added

comment:15 by JosefAssad, 15 years ago

Cc: joe@… added

comment:16 by Chris Beaven, 15 years ago

Patch needs improvement: set

This needs to be brought up to speed with the thread-safe changes to template tags (cycle uses the RenderContext now).

comment:17 by Chris Chambers, 14 years ago

Cc: Chris Chambers added

comment:18 by Gabriel Hurley, 14 years ago

Severity: Normal
Type: Cleanup/optimization

comment:19 by Jeff Schroeder, 14 years ago

Cc: jeffschroeder@… added
Easy pickings: unset

by jamesp, 13 years ago

Attachment: 5908-resetcycle.3.diff added

Updated patch to current trunk; added TemplateSyntaxError tests

comment:20 by jamesp, 13 years ago

UI/UX: unset

Updated the patch to the current trunk but I am wondering if {% resetcycle %} shouldn't reset the most recent cycle if a name isn't provided.
This would be consistent with {% endblock %} and may lead to confusion if not implemented similarly.

comment:21 by wim@…, 12 years ago

James, what do you think of changing the name to endcycle, like endblock and endcomment ?

comment:22 by isaacsutherland@…, 12 years ago

Cc: isaacsutherland@… added

by b.schube@…, 12 years ago

Attachment: 5908-resetcycle.4.diff added

Updated patch to current trunk

by b.schube@…, 12 years ago

Attachment: 5908-resetcycle.5.diff added

small correction

comment:23 by Florian Apolloner, 12 years ago

Changed the cycle tag to always reset the "last" cycle instead of the "last unnamed".

by Florian Apolloner, 12 years ago

Attachment: t5908.diff added

comment:24 by Sergei Maertens, 8 years ago

Owner: changed from nobody to Sergei Maertens
Status: newassigned

comment:25 by Tim Graham, 8 years ago

Patch needs improvement: unset

comment:26 by Tim Graham, 8 years ago

Patch needs improvement: set
Summary: Cycle tag should reset after it steps out of scope(?)Add a resetcycle template tag
Type: Cleanup/optimizationNew feature

Left some comments for improvement on the PR.

comment:27 by Sergei Maertens, 8 years ago

Patch needs improvement: unset

Remarks processed, up for re-review

comment:28 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 32c02f2a:

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.

Note: See TracTickets for help on using tickets.
Back to Top