django template filters change order of evaluation
-
12-06-2021 - |
Question
Is there a way to change the order django evaluates a template filter ?
Say i have
{{ 3|add:5|multiply:"10" }}
Right now his adds 3+5 and multiplies the result times 10.
What i am looking for is a way of doing the following:
{{ 3|add:(5|multiply:"10") }}
As you can see i wrapped the 5|multiply:"10"
in parenthesis to emphasize that it should be evaluated before the |add
. Is this possible ?
Solution
You have two possibilities:
You could change the order of filters/values:
{{ 5|multiply:10|add:3 }}
Or You could use {% with %}
:
{% with temp=5|multiply:10 %}
{{ 3|add:temp }}
{% endwith %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow