I have managed to call python functions from jinja2 by using custom filters, but I can only seem to call functions with one or more parameters. In the following I have had to pass a junk parameter in order to treat ctest as a function rather than a variable.
It also doesn’t work if I just call {{ ctest() }}.
Is there a different way to force this to be a function call or should I be using a different approach?
code:
def ctest(stuff):
return "yeah!"
template_env = jinja2.Environment (loader = jinja2.FileSystemLoader(template_file_root))
#custom filters
template_env.filters['ctest'] = ctest
template:
Working? {{ junk|ctest }}
output:
working? yeah!
Summarizing the comments into an answer:
The ability to call functions by adding it to filters isn’t really the correct way of going about this since (as Wooble pointed out) I’m not looking to filter anything.
Instead the function just needs to be added to the template_env.globals: