Opened 7 years ago

Closed 18 months 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: master
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


When you need to access fields on objects in a {% firstof %} fashion, semantically 'optional' fields can cause issues:

    Today the teacher is {% firstof %},
    and drives a {% firstof %}

If substitute doesn't have a value for car , then it will fall back to .

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 %}
        and drives a {{ }}
    {% 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 7 years ago.
Patch to and templatetag docs

Download all attachments as: .zip

Change History (11)

Changed 7 years ago by Tim Valenta

Attachment: firstof_as.diff added

Patch to and templatetag docs

comment:1 Changed 7 years ago by Tim Valenta

Needs documentation: unset
Needs tests: set
Patch needs improvement: unset

comment:2 Changed 7 years ago by Alex Robbins

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 Changed 7 years ago by Russell Keith-Magee

milestone: 1.2
Triage Stage: UnreviewedDesign decision needed

comment:4 Changed 5 years ago by Matt McClanahan

Severity: Normal
Type: New feature

comment:5 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:6 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:7 Changed 4 years ago by Aymeric Augustin

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 Changed 3 years ago by mark0978@…

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 Changed 18 months ago by craigls

Owner: changed from nobody to craigls
Status: newassigned

comment:10 Changed 18 months ago by Tim Graham <timograham@…>

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