I need a single template, so I choose not to create a specific environment. Jinja creates an environment under the hood, so I attempted to add the filter to that environment. As in the following example:
TEMPLATE = """
{{items|round_to_even}}
"""
def main():
template = Template(TEMPLATE, trim_blocks=True, lstrip_blocks=True)
template.environment.filters.update(
{
'round_to_even': lambda x: x if x % 2 == 0 else x+1,
}
)
print(template.render(
items=[1, 2, 3],
))
return 0
if __name__ == '__main__':
main()
I was expecting an output of [2, 2, 4]
or something along these lines; instead, I got an error:
jinja2.exceptions.TemplateAssertionError: No filter named 'round_to_even'.
I tried looking through the source code, but couldn't find a way that looked intended to add filters. What would be the way to add custom filters to a template?