Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#31024 closed Cleanup/optimization (fixed)

firstof template tag documentation incorrectly specifies value testing.

Reported by: Keryn Knight Owned by: Uttam Kini
Component: Documentation Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

The documentation for {%firstof x y z %} says:

Outputs the first argument variable that is not False. Outputs nothing if all the passed variables are False.

The emphasis not mine, and indicates it's specifically testing for the singleton False. But the example it gives as an equivalent is based on normal truth value testing:

{% if var1 %}
    {{ var1 }}
{% elif var2 %}
    {{ var2 }}
{% elif var3 %}
    {{ var3 }}
{% endif %}

and the code is also based on truthiness

The documentation for how a simple {% if ... %} behaves meanwhile more correctly prescribes it's function related to "true" and what that means.

evaluates a variable, and if that variable is “true” (i.e. exists, is not empty, and is not a false boolean value) the contents of the block are output

Were it actually based on False rather than Falsiness, the following would not output truthy.

>>> from django.template import Template, Context
>>> context = {'a': None,'b': False,'c': 0,'d': '','e': (),'f': [],'g': {},'h': set(),'i': "truthy"}
>>> t = Template("{% firstof a b c d e f g h i %}")
>>> t.render(Context(context))
'truthy'

Suggestion is to clarify that it's based on truthiness/falsiness, rather than imply it's directly based on encountering False.

Change History (7)

comment:1 by Mariusz Felisiak, 5 years ago

Easy pickings: set
Summary: firstof template tag documentation incorrectly specifies value testingfirstof template tag documentation incorrectly specifies value testing.
Triage Stage: UnreviewedAccepted

comment:2 by Uttam Kini, 5 years ago

Owner: changed from nobody to Uttam Kini
Status: newassigned

comment:4 by Mariusz Felisiak, 5 years ago

Has patch: set
Patch needs improvement: set

comment:5 by Mariusz Felisiak, 5 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 84936ac2:

[3.0.x] Fixed #31024 -- Clarified {% firstof %} tag's handling of arguments.

Backport of d646e3d14fa04b5081476b84f4500c8651c167c2 from master

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In d646e3d:

Fixed #31024 -- Clarified {% firstof %} tag's handling of arguments.

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