Call a python function from jinja2
I am using jinja2, and I want to call a python function as a helper, using a similar syntax as if I were calling a macro. jinja2 seems intent on preventing me from making a function call, and insists I repeat myself by copying the function into a template as a macro.
Is there any straightforward way to do this? And, is there any way to import a whole set of python functions and have them accessible from jinja2, without going through a whole lot of rigamarole (such as writing an extension)?
For those using Flask, put this in your __init__.py
:
def clever_function():
return u'HELLO'
app.jinja_env.globals.update(clever_function=clever_function)
and in your template call it with {{ clever_function() }}
Note: This is Flask specific!
I know this post is quite old, but there are better methods of doing this in the newer versions of Flask using context processors.
Variables can easily be created:
@app.context_processor
def example():
return dict(myexample='This is an example')
The above can be used in a Jinja2 template with Flask like so:
{{ myexample }}
(Which outputs This is an example
)
As well as full fledged functions:
@app.context_processor
def utility_processor():
def format_price(amount, currency=u'€'):
return u'{0:.2f}{1}'.format(amount, currency)
return dict(format_price=format_price)
The above when used like so:
{{ format_price(0.33) }}
(Which outputs the input price with the currency symbol)
Alternatively, you can use jinja filters, baked into Flask. E.g. using decorators:
@app.template_filter('reverse')
def reverse_filter(s):
return s[::-1]
Or, without decorators, and manually registering the function:
def reverse_filter(s):
return s[::-1]
app.jinja_env.filters['reverse'] = reverse_filter
Filters applied with the above two methods can be used like this:
{% for x in mylist | reverse %}
{% endfor %}
I think jinja deliberately makes it difficult to run 'arbitrary' python within a template. It tries to enforce the opinion that less logic in templates is a good thing.
You can manipulate the global namespace within an Environment
instance to add references to your functions. It must be done before you load any templates. For example:
from jinja2 import Environment, FileSystemLoader
def clever_function(a, b):
return u''.join([b, a])
env = Environment(loader=FileSystemLoader('/path/to/templates'))
env.globals['clever_function'] = clever_function
from jinja2 import Template
def custom_function(a):
return a.replace('o', 'ay')
template = Template('Hey, my name is {{ custom_function(first_name) }} {{ func2(last_name) }}')
template.globals['custom_function'] = custom_function
You can also give the function in the fields as per Matroskin's answer
fields = {'first_name': 'Jo', 'last_name': 'Ko', 'func2': custom_function}
print template.render(**fields)
Will output:
Hey, my name is Jay Kay
Works with Jinja2 version 2.7.3
And if you want a decorator to ease defining functions on template.globals
check out Bruno Bronosky's answer