擅长:python、mysql、java
<p>从模板中调用函数的一种方法是使用<code>@app.context_processor</code>decorator。</p>
<p>在python文件中,比如main.py</p>
<pre><code>@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)
</code></pre>
<p>在像footer.html这样的html文件中</p>
<pre><code><p> Copyright {{ company() }} 2005 - {{ date_now("%Y") }} </p>
</code></pre>
<p>输出</p>
<pre><code>Copyright ABC Pvt. Ltd. 2005 - 2015
</code></pre>