Django

Code

Ticket #4534 (closed: fixed)

Opened 1 year ago

Last modified 4 months ago

ifchanged could use an else tag

Reported by: Bill Fenner <fenner@gmail.com> Assigned to: nobody
Milestone: Component: Template system
Version: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

I want to group items with like values for ballot_id together. I thought ifchanged would be my savior, so I tried to write

<tr bgcolor="
{% ifchanged match.ballot_id %}
  {% cycle F8D6F8,E2AFE2 as ballotcolor %}
{% else %}
  {{ ballotcolor }}
{% endif %}
">

Unfortunately, ifchanged doesn't take an else tag. Can it?

Attachments

ifchanged-else.patch (4.6 kB) - added by SmileyChris on 06/11/07 17:39:25.
with tests and docs

Change History

06/11/07 15:35:45 changed by ubernostrum

  • status changed from new to closed.
  • needs_better_patch changed.
  • resolution set to duplicate.
  • needs_tests changed.
  • needs_docs changed.

Diplicate of #4474.

06/11/07 15:52:28 changed by jacob

  • status changed from closed to reopened.
  • resolution deleted.
  • stage changed from Unreviewed to Design decision needed.

06/11/07 17:39:25 changed by SmileyChris

  • attachment ifchanged-else.patch added.

with tests and docs

06/11/07 17:40:37 changed by SmileyChris

  • has_patch set to 1.

The example given in the ticket description won't work because any changes to context variables are discarded between loops.

06/13/07 13:18:12 changed by Bill Fenner <fenner@gmail.com>

With this patch, my actual use case is quite close:

(whitespace added)

{% load smiley_ifchanged %}
        <tr bgcolor="#
{% ifchanged match.ballot_id %}
  {% cycle F8D6F8,E2AFE2 as ballotcolor %}
{% else %}
  {% if match.synthetic %}
    {% cycle ballotcolor %}
  {% else %}
    {{ ballotcolor }}
  {% endif %}
{% endifchanged %}">

The cycle occurs on the rows that I want it to, but unfortunately the other rows (as you predicted) didn't get a value.

I was surprised to see that {{ ballotcolor }} didn't work; it works in the following construct:

        <tr bgcolor="#
{% if match.primary_flag %}
  {% cycle F8D6F8,E2AFE2 as ballotcolor %}
{% else %}
  {{ ballotcolor }}
{% endif %}"> 

06/13/07 13:23:54 changed by Bill Fenner <fenner@gmail.com>

I've read the code further and now I understand why the context changes are discarded with the ifchanged tag (but not with if).

11/30/07 15:02:19 changed by jacob

  • stage changed from Design decision needed to Accepted.

11/30/07 15:08:41 changed by SmileyChris

  • needs_docs set to 1.

05/08/08 16:15:11 changed by zellyn

For what it's worth, here's a use case for this: Anytime you want not headers that change but dividers.

For instance, if you want to print a red line when the day changes and a blue line when the hour changes between comments in a chat log. Without "else" on "ifchanged", it's difficult to suppress the blue hour-change line when the day changes.

(follow-up: ↓ 10 ) 07/19/08 16:57:22 changed by Dave Smith <davewsmith@gmail.com>

I've used the equivalent of this patch for three different use cases, and found it very useful.

- showing " to mark duplicates in a column of numbers (that's what the customer wanted) - changing CSS to use a different style for duplicates - dropping an image in place of a duplicate.

With an 'else', ifchanged is really easy to use for stuff like this, esp. since changes are limited to the template.

The patch installs cleanly in r7997 and the tests pass.

(in reply to: ↑ 9 ) 07/20/08 18:44:25 changed by SmileyChris

Some good use cases, and jacob accepted it anyway. I'm going to promote to checkin.

07/20/08 18:45:55 changed by SmileyChris

  • needs_docs deleted.
  • version deleted.
  • stage changed from Accepted to Ready for checkin.

I'm not sure why I marked as needs docs, since I put in docs in the patch. Maybe they should have been clearer? A committer can make that call though.

07/26/08 17:09:43 changed by mtredinnick

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

(In [8095]) Fixed #4534 -- Added an "else" option to the "ifchanged" template tag. Patch from SmileyChris?.


Add/Change #4534 (ifchanged could use an else tag)




Change Properties
Action