I guess just generally I’m curious about what operations are allowable in jinja2 brackets, e.g. what I’m trying to do is perform an operation on embedded data like so:
{{ round(255*(mileage['chevy'] - mileage['ford']))/1000 }}
This throws the error on traceback:
UndefinedError: ’round’ is undefined
Similarly when I try to use ‘abs’ in a bracketed jinja block, I get an Undefined error–even though they’re both standard lib functions. Is there some way to perform this operation during template-rendering, rather than before passing the data?
The jinja2 templating language is different from the python language. In jinja2, operation on values are often done during filters :
{{ something | operation }}. You can find a list of filters in the jinja2 documentation.For example, to round, you can do :
This will display “42” on the web page. A
absfilter exist in the same way.Please note that these filters can only be used to alter values before display, and can be used for calculations. Calculations shouldn’t be done in the template anyway.