Django

Code

Ticket #7501 (closed: fixed)

Opened 2 years ago

Last modified 3 months ago

template rendering doesn't reset cycle tag

Reported by: mitsuhiko Assigned to: nobody
Milestone: Component: Template system
Version: SVN Keywords:
Cc: elsdoerfer@gmail.com Triage Stage: Design decision needed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

The cycle iter is stored on the node. Unless templates are re-parsed each request that will mean that multiple template renderings share the same cycle iter. As a result of that it is very likely that cycle skips items on renderings.

Proposed fix: store the iter on the context object.

Attachments

fixcyclebehaviour.patch (2.5 kB) - added by steveire on 09/05/09 09:02:04.
Fixes cycle reset issue by adding reset feature to node.

Change History

07/29/08 15:00:57 changed by anonymous

  • cc set to elsdoerfer@gmail.com.
  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

08/08/08 16:23:14 changed by ericholscher

  • stage changed from Unreviewed to Design decision needed.
  • summary changed from {% cycle %} is not threadsafe to template rendering doesn't reset cycle tag.
  • milestone changed from 1.0 to post-1.0.

10/03/08 15:27:43 changed by SmileyChris

In #5908 Malcolm talks about the importance of keeping the state of the internal counter. Currently, the {% for %} tag (and potentially others) sets up a local context for each iteration, so if we put the counter straight on the context, it'll be lost the next iteration.

I think Context needs a way to set a "global" context variable (if you understand the internals, probably just stuffing it at the highest level context).

(follow-up: ↓ 5 ) 01/04/09 05:14:47 changed by harryman100

There's a workaround for this which can be used in templates:

Using the 'cycle as' syntax resets the variable. By outputting the 'cycle as' inside a comment it hides it from the rendering. It's not perfect, but it works in most cases

<!--{% cycle 'rowB' 'rowA' as rowstyle %}-->
{% for post in posts %}
        <tr class="{% cycle rowstyle %}">
        ...
{% endfor %}

(in reply to: ↑ 4 ) 01/04/09 05:29:19 changed by harryman100

Replying to harryman100: Scrap that, it was fluke that it worked with a particular dataset

02/25/09 13:51:44 changed by

  • milestone deleted.

Milestone post-1.0 deleted

06/09/09 11:14:36 changed by kmtracey

#11290 reported this again.

06/25/09 08:47:26 changed by bpeschier

I came across this a few times when rendering multiple tables next to each other with the same cycle (with 2 arguments for colouring) on a row. A double loop. If a table had an even amount of rows, the colours matched, otherwise, a grid appeared. It came out ugly sometimes, but acceptable.

Looking for a quick fix, I suspected there must be a reset of some sort, but there is not.

The {% cycle [] as var %} could be a nice way to fix this. But for starters, the output of {% cycle [] as var %} should go away. "as var" in other tags denotes a variable for later use. If we can reference the cycle-variable on the node, we could make a {% cycle reset var %} of some sort, which would reset the cycle referenced.

09/05/09 08:57:40 changed by steveire

  • has_patch set to 1.

The attached patch fixes the issue and passes the test attached to #11290.

The test and fix are attached in the same patch here for completeness.

09/05/09 09:02:04 changed by steveire

  • attachment fixcyclebehaviour.patch added.

Fixes cycle reset issue by adding reset feature to node.

09/06/09 20:44:24 changed by SmileyChris

This would be fixed by #6262 - and I consider the ticket there to contain the correct fix to this issue.

12/14/09 18:14:50 changed by russellm

  • status changed from new to closed.
  • resolution set to fixed.

Resolved by [11862]


Add/Change #7501 (template rendering doesn't reset cycle tag)




Change Properties
Action