Opened 14 years ago

Closed 9 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)

firstof_as.diff (3.3 KB ) - added by Tim Valenta 14 years ago.
Patch to defaulttags.py and templatetag docs

Download all attachments as: .zip

Change History (11)

by Tim Valenta, 14 years ago

Attachment: firstof_as.diff added

Patch to defaulttags.py and templatetag docs

comment:1 by Tim Valenta, 14 years ago

Needs tests: set

comment:2 by Alex Robbins, 14 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 Russell Keith-Magee, 14 years ago

milestone: 1.2
Triage Stage: UnreviewedDesign decision needed

comment:4 by Matt McClanahan, 13 years ago

Severity: Normal
Type: New feature

comment:5 by Aymeric Augustin, 12 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:6 by Aymeric Augustin, 12 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:7 by Aymeric Augustin, 11 years ago

Triage Stage: Design decision neededAccepted

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 mark0978@…, 11 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 craigls, 9 years ago

Owner: changed from nobody to craigls
Status: newassigned

comment:10 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 75bc5bc6:

Fixed #12199 -- Added the ability to use "as" with the firstof template tag.

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