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
I like @AJP's answer. I used it verbatim until I ended up with a lot of functions. Then I switched to a Python function decorator.
from jinja2 import Template
template = '''
Hi, my name is {{ custom_function1(first_name) }}
My name is {{ custom_function2(first_name) }}
My name is {{ custom_function3(first_name) }}
'''
jinga_html_template = Template(template)
def template_function(func):
jinga_html_template.globals[func.__name__] = func
return func
@template_function
def custom_function1(a):
return a.replace('o', 'ay')
@template_function
def custom_function2(a):
return a.replace('o', 'ill')
@template_function
def custom_function3(a):
return 'Slim Shady'
fields = {'first_name': 'Jo'}
print(jinga_html_template.render(**fields))
Good thing functions have a __name__
!
Never saw such simple way at official docs or at stack overflow, but i was amazed when found this:
# jinja2.__version__ == 2.8
from jinja2 import Template
def calcName(n, i):
return ' '.join([n] * i)
template = Template("Hello {{ calcName('Gandalf', 2) }}")
template.render(calcName=calcName)
# or
template.render({'calcName': calcName})
There's a much simpler decision.
@app.route('/x')
def x():
return render_template('test.html', foo=y)
def y(text):
return text
Then, in test.html:
{{ foo('hi') }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With