Opened 18 years ago
Closed 9 years ago
#5908 closed New feature (fixed)
Add a resetcycle template tag
| Reported by: | 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)
Change History (35)
comment:1 by , 18 years ago
| Cc: | added | 
|---|
comment:2 by , 18 years ago
| Triage Stage: | Unreviewed → Design decision needed | 
|---|
by , 18 years ago
comment:3 by , 18 years ago
| Has patch: | set | 
|---|
comment:4 by , 18 years ago
| Triage Stage: | Design decision needed → Accepted | 
|---|
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 , 18 years ago
| Cc: | added | 
|---|
comment:6 by , 18 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 , 18 years ago
| Cc: | added | 
|---|
comment:8 by , 18 years ago
I've just ran into this very problem, this needs a solution before 1.0 IMHO.
comment:9 by , 18 years ago
| Cc: | added | 
|---|
comment:11 by , 17 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 , 17 years ago
| Attachment: | 5908_resetcycle.2.diff added | 
|---|
Patch: add the {% resetcycle %} tag, with tests and documentation
comment:12 by , 17 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 , 16 years ago
| Cc: | added | 
|---|
comment:14 by , 16 years ago
| Cc: | added | 
|---|
comment:15 by , 16 years ago
| Cc: | added | 
|---|
comment:16 by , 16 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 , 15 years ago
| Cc: | added | 
|---|
comment:18 by , 15 years ago
| Severity: | → Normal | 
|---|---|
| Type: | → Cleanup/optimization | 
comment:19 by , 14 years ago
| Cc: | added | 
|---|---|
| Easy pickings: | unset | 
by , 14 years ago
| Attachment: | 5908-resetcycle.3.diff added | 
|---|
Updated patch to current trunk; added TemplateSyntaxError tests
comment:20 by , 14 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 , 13 years ago
James, what do you think of changing the name to endcycle, like endblock and endcomment ?
comment:22 by , 13 years ago
| Cc: | added | 
|---|
comment:23 by , 12 years ago
Changed the cycle tag to always reset the "last" cycle instead of the "last unnamed".
by , 12 years ago
| Attachment: | t5908.diff added | 
|---|
comment:24 by , 9 years ago
| Owner: | changed from to | 
|---|---|
| Status: | new → assigned | 
comment:26 by , 9 years ago
| Patch needs improvement: | set | 
|---|---|
| Summary: | Cycle tag should reset after it steps out of scope(?) → Add a resetcycle template tag | 
| Type: | Cleanup/optimization → New feature | 
Left some comments for improvement on the PR.
New safe_cycle tag which respects for loop nesting