Opened 18 years ago
Closed 16 years ago
#4534 closed (fixed)
ifchanged could use an else tag
Reported by: | 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)
Change History (13)
comment:1 by , 18 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
comment:2 by , 18 years ago
Resolution: | duplicate |
---|---|
Status: | closed → reopened |
Triage Stage: | Unreviewed → Design decision needed |
Under discussion on django-dev: http://groups.google.com/group/django-developers/browse_thread/thread/70323a07d047ad89
comment:3 by , 18 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 , 18 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 , 18 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 , 17 years ago
Triage Stage: | Design decision needed → Accepted |
---|
comment:7 by , 17 years ago
Needs documentation: | set |
---|
comment:8 by , 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.
follow-up: 10 comment:9 by , 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.
comment:10 by , 16 years ago
Some good use cases, and jacob accepted it anyway. I'm going to promote to checkin.
comment:11 by , 16 years ago
Needs documentation: | unset |
---|---|
Triage Stage: | Accepted → Ready 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 , 16 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
Diplicate of #4474.