Is it possible to import a Python module into a Jinja template so I can use its functions?
For example, I have a format.py file that contains methods for formatting dates and times. In a Jinja macro, can I do something like the following?
{% from 'dates/format.py' import timesince %}
{% macro time(mytime) %}
<a title="{{ mytime }}">{{ timesince(mytime) }}</a>
{% endmacro %}
Because format.py is not a template, the code above gives me this error:
UndefinedError: the template 'dates/format.py' (imported on line 2 in 'dates/macros.html') does not export the requested name 'timesince'
...but I was wondering if there was another way to achieve this.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…