从 Jinja 模板调用 Python 函数
Call a Python function from a Jinja template
有没有办法在 Jinja 模板中调用 Python 函数?该函数将只获取字符串 years
并将其转换为列表。
years = years.replace('[', '')
years = years.replace(']', '')
years = years.split(',')
如何在下面的模板中的 years
上调用它?
{% 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 %}
years
似乎是一个 JSON 列表,因此使用 json.loads
来解析它而不是手动剥离和拆分字符串。 years
貌似是视图发送给模板的变量,所以在视图中处理即可。
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)
如果您真的需要在模板中提供它(您可能不需要),您可以将 json.loads
添加到 Jinja 全局变量中。
app.add_template_global(json.loads, name='json_loads')
然后像普通函数一样在模板中使用它。
{{ macros.pagination_widget(pagination, '.yearresults', years=json_loads(years)) }}
从模板中调用函数的一种方法是使用 @app.context_processor
装饰器。
在 python 文件中 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)
在 html 文件中 footer.html
<p> Copyright {{ company() }} 2005 - {{ date_now("%Y") }} </p>
输出
Copyright ABC Pvt. Ltd. 2005 - 2015
可能如下:
首先在您的 main.py 文件中
def myYearLister(year):
return year.INTOLISTORWHATEVER
然后在 main.py 的某处包含以下内容(最好在函数之后执行)以使函数全局可访问
app.jinja_env.globals.update(myYearLister=myYearLister)
最后,您可以调用或使用模板中的函数
<div> {{ myYearLister(anything) }} </div>
有没有办法在 Jinja 模板中调用 Python 函数?该函数将只获取字符串 years
并将其转换为列表。
years = years.replace('[', '')
years = years.replace(']', '')
years = years.split(',')
如何在下面的模板中的 years
上调用它?
{% 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 %}
years
似乎是一个 JSON 列表,因此使用 json.loads
来解析它而不是手动剥离和拆分字符串。 years
貌似是视图发送给模板的变量,所以在视图中处理即可。
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)
如果您真的需要在模板中提供它(您可能不需要),您可以将 json.loads
添加到 Jinja 全局变量中。
app.add_template_global(json.loads, name='json_loads')
然后像普通函数一样在模板中使用它。
{{ macros.pagination_widget(pagination, '.yearresults', years=json_loads(years)) }}
从模板中调用函数的一种方法是使用 @app.context_processor
装饰器。
在 python 文件中 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)
在 html 文件中 footer.html
<p> Copyright {{ company() }} 2005 - {{ date_now("%Y") }} </p>
输出
Copyright ABC Pvt. Ltd. 2005 - 2015
可能如下:
首先在您的 main.py 文件中
def myYearLister(year):
return year.INTOLISTORWHATEVER
然后在 main.py 的某处包含以下内容(最好在函数之后执行)以使函数全局可访问
app.jinja_env.globals.update(myYearLister=myYearLister)
最后,您可以调用或使用模板中的函数
<div> {{ myYearLister(anything) }} </div>