Django maths using widthratio
27 Jun 2015I use a lot of Django templating and one of my pet peeves is it’s inability to handle simple maths. Seriously, why?
Sure, you can build your own functions for this, but in my opinion, maths is something that should be built-in. Yeah, you can add
, but that’s about it.
Now, if you’re only using integers, you can be quite creative with widthratio
to solve many math expressions:
Code
Caveats
- The code above has an extra space between
{
and%
and}
to avoid my Jekyll trying to parse it… - Remember
widthratio
rounds everything to integers. So, if you need precise results you should really look elsewhere.
Questions?
Have a question about this post, a project or anything else?
Let's have a conversation on Twitter.