Code

Opened 7 years ago

Closed 6 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: UI/UX:

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 SmileyChris 7 years ago.
with tests and docs

Download all attachments as: .zip

Change History (13)

comment:1 Changed 7 years ago by ubernostrum

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

Diplicate of #4474.

comment:2 Changed 7 years ago by jacob

  • Resolution duplicate deleted
  • Status changed from closed to reopened
  • Triage Stage changed from Unreviewed to Design decision needed

Changed 7 years ago by SmileyChris

with tests and docs

comment:3 Changed 7 years ago by SmileyChris

  • 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 Changed 7 years ago by Bill Fenner <fenner@…>

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 Changed 7 years ago by Bill Fenner <fenner@…>

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 Changed 6 years ago by jacob

  • Triage Stage changed from Design decision needed to Accepted

comment:7 Changed 6 years ago by SmileyChris

  • Needs documentation set

comment:8 Changed 6 years ago 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.

comment:9 follow-up: Changed 6 years ago by Dave Smith <davewsmith@…>

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.

comment:10 in reply to: ↑ 9 Changed 6 years ago by SmileyChris

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

comment:11 Changed 6 years ago by SmileyChris

  • Needs documentation unset
  • Triage Stage changed from Accepted to Ready for checkin
  • Version 0.96 deleted

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 Changed 6 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from reopened to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.