Opened 7 years ago

Closed 5 years ago

#11166 closed Bug (fixed)

{% widthratio 0 0 100 %} returns an empty string, rather than a 0

Reported by: Ludwig Pettersson Owned by: nobody
Component: Template system Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When supplying the {% widthratio %} template tag with both a value and max of 0 - you get an empty string returned (), rather than a 0. Mathematically, this is correct - since typically division by zero results in undefined, but I wouldn't say this is the expected behaviour when using this to create charts, where a 0 rather than nothing, would be expected.

Example:

<img width="{% widthratio 0 0 100 %}" height="10" ... />
<img style="width:{% widthratio 0 0 100 %}px; height:10px" ... />

This would produce:

<img width="" height="10" ... />
<img style="width:px; height:10px" ... />

Now, in the first example, the difference is only semantic - but the second one creates invalid css as well, not such a big of a deal, but, gets troublesome when you *need* a number returned, such as when using {% widthratio %} with the google charts api. (sparklines, for example, require a number between 0-100 - anything above 100, such as 550, is visually returned as 100 in the chart - so you can't have empty values.)

But, when would anyone ever encounter this problem? I personally do when displaying data over a time period, where in some cases, nothing may have happened - leaving everything at 0, both the values and max values - which then results in everything showing up empty, rather than defaulting back to 0.

I realize this is easy to work around, but I thought I would put it out there since at least I wasn't expecting this behavior and it seems a bit weird, especially when this is mainly used to create charts in templates.

(Patch is for the latest svn checkout as of this writing, rev 10833)

Attachments (1)

django-diff.txt (1.4 KB) - added by Ludwig Pettersson 7 years ago.
svn diff of patch

Download all attachments as: .zip

Change History (6)

Changed 7 years ago by Ludwig Pettersson

Attachment: django-diff.txt added

svn diff of patch

comment:1 Changed 7 years ago by Ludwig Pettersson

Component: UncategorizedTemplate system
Has patch: set
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Version: 1.0SVN

comment:2 Changed 7 years ago by Chris Beaven

Triage Stage: UnreviewedDesign decision needed

comment:3 Changed 5 years ago by Julien Phalip

Severity: Normal
Type: Bug

comment:4 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset
Triage Stage: Design decision neededAccepted
UI/UX: unset

I agree that the empty string is technically the "correct" output (the template engine returns nothing whenever an exception occurs), but that in this case "practicality beats purity".

comment:5 Changed 5 years ago by Aymeric Augustin

Resolution: fixed
Status: newclosed

In [17224]:

Fixed #11166 -- {% widthratio %} should return 0 when the maximum is 0, no matter the value.

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