Opened 17 years ago

Closed 16 years ago

#4534 closed (fixed)

ifchanged could use an else tag

Reported by: Bill Fenner <fenner@…> Owned by: nobody
Component: Template system Version:
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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 (1)

ifchanged-else.patch (4.6 KB ) - added by Chris Beaven 17 years ago.
with tests and docs

Download all attachments as: .zip

Change History (13)

comment:1 by James Bennett, 17 years ago

Resolution: duplicate
Status: newclosed

Diplicate of #4474.

comment:2 by Jacob, 17 years ago

Resolution: duplicate
Status: closedreopened
Triage Stage: UnreviewedDesign decision needed

by Chris Beaven, 17 years ago

Attachment: ifchanged-else.patch added

with tests and docs

comment:3 by Chris Beaven, 17 years ago

Has patch: set

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

comment:4 by Bill Fenner <fenner@…>, 17 years ago

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 %}"> 

comment:5 by Bill Fenner <fenner@…>, 17 years ago

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

comment:6 by Jacob, 17 years ago

Triage Stage: Design decision neededAccepted

comment:7 by Chris Beaven, 17 years ago

Needs documentation: set

comment:8 by Zellyn Hunter, 17 years ago

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.

comment:9 by Dave Smith <davewsmith@…>, 16 years ago

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 comment:10 by Chris Beaven, 16 years ago

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

comment:11 by Chris Beaven, 16 years ago

Needs documentation: unset
Triage Stage: AcceptedReady for checkin
Version: 0.96

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.

comment:12 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: reopenedclosed

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

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