有没有办法在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 %}
company=name
可以直接写成company="ABC Pvt. Ltd."
,然后在footer.html
中只需要写{{ company }}
。但是如果以后需要扩展或复杂化,你的例子就非常好了。只是为了返回一些常量字符串,它不必调用函数(为了未来可能会感到困惑的读者)。 - jeromej