Is there a way to call a Python function inside a Jinja template? The function will just take the string years
and turn it into a list.
years = years.replace('[', '')
years = years.replace(']', '')
years = years.split(',')
How can I call this on years
in the template below?
{% extends "base.html" %}
{% import "_macros.html" as macros %}
{% block title %}Year Results{% endblock %}
{% block page_content %}
<div class="page-header">
<h1>Year Search Results</h1>
</div>
<ul class=entries>
{% for entry in entries %}
<li><h3><a href="{{ url_for('main.grantinfo', applid=entry.appl_id) }}">{{ entry.appl_id }} : {{ entry.project_title }}</a></h3>
<br>
{% else %}
<li><em>No entry here</em>
{% endfor %}
</ul>
{% if pagination %}
<div class="pagination">
{{ macros.pagination_widget(pagination, '.yearresults', years=years) }}
</div>
{% endif %}
{% endblock %}
What might be worth knowing is the fact that you can pass a Python function into your Jinja templates. Doing this can greatly improve the readability of your template as well as allow you to handle more complicated scenario's.
Jinja works with Python 2.7. x and >= 3.5. If you are using Python 3.2 you can use an older release of Jinja (2.6) as support for Python 3.2 was dropped in Jinja version 2.7. The last release which supported Python 2.6 and 3.3 was Jinja 2.10.
from_string . Jinja 2 provides a Template class that can be used to do the same, but with optional additional configuration. Jinja 1 performed automatic conversion of bytes in a given encoding into unicode objects.
The first step to use Jinja in Django is to install the core package with the pip command: python3 -m pip install Jinja2 . Note the package name is Jinja2 while the latest Jinja version is 3.0.
A way to call functions from within the template is using @app.context_processor
decorator.
In python file like main.py
@app.context_processor
def my_utility_processor():
def date_now(format="%d.m.%Y %H:%M:%S"):
""" returns the formated datetime """
return datetime.datetime.now().strftime(format)
def name():
""" returns bulshit """
return "ABC Pvt. Ltd."
return dict(date_now=date_now, company=name)
In html file like footer.html
<p> Copyright {{ company() }} 2005 - {{ date_now("%Y") }} </p>
Output
Copyright ABC Pvt. Ltd. 2005 - 2015
years
appears to be a JSON list, so use json.loads
to parse it rather than stripping and splitting strings manually. years
appears to be a variable sent from the view to the template, so just do the processing in the view.
years = json.loads(years)
# years string "[1999, 2000, 2001]"
# becomes list [1999, 2000, 2001]
# without parsing the string manually
return render_template('years.html', years=years)
If you really need to make this available in templates (you probably don't), you can add json.loads
to the Jinja globals.
app.add_template_global(json.loads, name='json_loads')
Then use it in a template like a normal function.
{{ macros.pagination_widget(pagination, '.yearresults', years=json_loads(years)) }}
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