Opened 7 years ago

Closed 7 years ago

#28213 closed New feature (wontfix)

New template filter like filesizeformat but for other units

Reported by: Nicolas Bruot Owned by: nobody
Component: Template system Version: 1.11
Severity: Normal Keywords: Template filter
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

File sizes are not the only numbers with a unit that one may encounter when developing an app. Would it make sense to add a more generic filter that acts on a number similarly to fileformatsize but for other units?

Rough idea of the behavior of the filter:

  • Takes a number as input (int, float, positive or negative)
  • Finds the most adapted 103n unit: m (10-3), nothing (1), k (103), M (106)...
  • Divides the number by 103n and localizes it
  • Append the unit

The simplest way to do it may be for the filter to generate strings with just the power prefix like "123.4 k", "-20 m", and the rest of the unit can then be outside the tag, for example:

{% 123400|human %}m = 123.4 km (a length)
{% 123400|human %}g = 123.4 kg (a mass)

However, this strategy makes it difficult for users to fix edge cases they may encounter. Instead, it might be worth to include the whole unit in the tag, in case the power prefix cannot be separated from the unit suffix (or when the suffix also depend on the power):

  • Suffix and prefix may not be separated in some languages (I don't know).
  • Happens for users that prefer full unit string for numbers < 1000, for example "123 km", but "123 meters" (the same way that filesizeformat uses "bytes" instead of "b" for a size below 1024).

In such cases, we need to provide a way for the user to specify the unit strings for all the powers (or the other way around: the filter could spit out the power as well as the number, and the user can then work out from that whatever he wants to do).

Change History (1)

comment:1 by Tim Graham, 7 years ago

Resolution: wontfix
Status: newclosed

I don't think Django is going to add more built in template tags. There are lots of third-party libraries which can be developed more rapidly outside of Django.

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