Opened 15 years ago
Closed 10 years ago
#12199 closed New feature (fixed)
Give 'firstof' tag ability to assign result to context
Reported by: | Tim Valenta | Owned by: | craigls |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Normal | Keywords: | firstof, as, context |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | yes | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When you need to access fields on objects in a {% firstof %}
fashion, semantically 'optional' fields can cause issues:
Today the teacher is {% firstof substitute.name teacher.name %}, and drives a {% firstof substitute.car teacher.car %}
If substitute
doesn't have a value for car
, then it will fall back to teacher.car
.
The real issue is that {% firstof %}
cannot preserve its value for later multiple inspections. If the {% firstof %}
tag could assign its result to a context variable, it would solve this problem:
{% firstof substitute teacher as teacher_for_today %} Today the teacher is {{ teacher_for_today }} {% if teacher_for_today.car %} and drives a {{ teacher_for_today.car }} {% endif %}
I am unsure that this is possible to achieve without the as
syntax. One would have to code a template tag or filter for the sole purpose of duplicating the exact functionality that I propose be added to {% firstof %}
.
Attached patch for functionality and documentation on the subject.
Attachments (1)
Change History (11)
by , 15 years ago
Attachment: | firstof_as.diff added |
---|
comment:1 by , 15 years ago
Needs tests: | set |
---|
comment:2 by , 15 years ago
I worked on this ticket during the sprint on 12/12/09.
There was initially some concern that this ticket would accomplish the same thing as the proposed capture tag. They would do exactly the same thing though.
Jacob is -0 on this ticket since we are making the tag a switch statement, but didn't want to veto it without discussion.
comment:3 by , 15 years ago
milestone: | 1.2 |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
comment:4 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
comment:7 by , 12 years ago
Triage Stage: | Design decision needed → Accepted |
---|
I don't know what the "capture" tag discussed 3 years ago was, but AFAIK it didn't come into existence.
This feature can be useful and it's still quite restricted -- it isn't a general switch statement.
comment:8 by , 12 years ago
There is another instance where the AS keyword would be very useful
Trying to write a template to include something for xyz: in json, but i can't put {% firstof %} output thru escapejs as it currently stands.
xyz: '{% firstof a b %}',
with AS, we could do:
{% firstof a b AS c %} xyz: '{{ c|escapejs }}',
Right now to do this you end up with a logic block in the template.
{% if a %}{{ a|escapejs }}{% else %}{{ b|escapejs }}{% endif %}
which isn't horrible until you only want a or b if EITHER of them is defined and then it looks like:
{% if a or b %}xyz: '{% if a %}{{ a|escapejs }}{% else %}{{ b|escapejs }}{% endif %}',{% endif %}
while it could be as simple as:
{% firstof a b AS c %}{% if c %}xyz: '{{ c|escapejs }}',{% endif %}
comment:9 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Patch to defaulttags.py and templatetag docs