Opened 6 years ago

Closed 3 years ago

#11166 closed Bug (fixed)

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

Reported by: luddep 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 luddep 6 years ago.
svn diff of patch

Download all attachments as: .zip

Change History (6)

Changed 6 years ago by luddep

svn diff of patch

comment:1 Changed 6 years ago by luddep

  • Component changed from Uncategorized to Template system
  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Version changed from 1.0 to SVN

comment:2 Changed 6 years ago by SmileyChris

  • Triage Stage changed from Unreviewed to Design decision needed

comment:3 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:4 Changed 3 years ago by aaugustin

  • Easy pickings unset
  • Triage Stage changed from Design decision needed to Accepted
  • 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 3 years ago by aaugustin

  • Resolution set to fixed
  • Status changed from new to closed

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