将 Python 模块导入 Jinja 模板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4828406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:42:18  来源:igfitidea点击:

Import a Python module into a Jinja template?

pythonjinja2python-import

提问by Matt Norris

Is it possible to import a Python module into a Jinja template so I can use its functions?

是否可以将 Python 模块导入 Jinja 模板中以便我可以使用其功能?

For example, I have a format.pyfile that contains methods for formatting dates and times. In a Jinja macro, can I do something likethe following?

例如,我有一个format.py文件,其中包含格式化日期和时间的方法。在神社宏,我可以做一些下面?

{% from 'dates/format.py' import timesince %}

{% macro time(mytime) %}
<a title="{{ mytime }}">{{ timesince(mytime) }}</a>
{% endmacro %}

Because format.pyis not a template, the code above gives me this error:

因为format.py不是模板,所以上面的代码给了我这个错误:

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.

...但我想知道是否有另一种方法可以实现这一目标。

采纳答案by Wooble

Within the template, no, you cannot import python code.

在模板中,不,您不能导入 python 代码。

The way to do this is to register the function as a jinja2 custom filter, like this:

这样做的方法是将该函数注册为 jinja2自定义过滤器,如下所示:

In your python file:

在你的 python 文件中:

from dates.format import timesince

environment = jinja2.Environment(whatever)
environment.filters['timesince'] = timesince
# render template here

In your template:

在您的模板中:

{% macro time(mytime) %}
<a title="{{ mytime }}">{{ mytime|timesince }}</a>
{% endmacro %}

回答by nightpool

Just pass the function into the template, like so

只需将函数传递到模板中,就像这样

from dates.format import timesince
your_template.render(timesince)

and in the template, just call it like any other function,

在模板中,像调用其他函数一样调用它,

{% macro time(mytime) %}
    <a title="{{ mytime }}">{{ timesince(mytime) }}</a>
{% endmacro %}

Functions are first-class citizens in python, so you can pass them around just like anything else. You could even pass in a whole module if you wanted.

函数在 python 中是一等公民,所以你可以像其他任何东西一样传递它们。如果需要,您甚至可以传入整个模块。

回答by Simon Black

You can export all of the symbols available in a module by providing the modules __dict__ as a parameter to the jinja template render method. The following will make available functions and types of __builtin__, inspect and types module to the template.

您可以通过将模块 __dict__ 作为参数提供给 jinja 模板渲染方法来导出模块中可用的所有符号。以下将使 __builtin__、inspect 和 types 模块的函数和类型可用于模板。

import __builtin__
import inspect
import types

env=RelEnvironment()
template = env.get_template(templatefile)

export_dict={}
export_dict.update(__builtin__.__dict__)
export_dict.update(types.__dict__)
export_dict.update(inspect.__dict__)

result=template.render(**export_dict)

Within template, to use a function of the exported modules similar to the following:

在模板中,使用类似于以下导出模块的功能:

{%- for element in getmembers(object) -%}
{# Use the getmembers function from inspect module on an object #}
{% endfor %}

回答by Skandix

A template doesn't know import, but you can teach it with the importlib:

模板不知道import,但您可以使用以下命令教授它importlib

import importlib
my_template.render( imp0rt = importlib.import_module )  # can't use 'import', because it's reserved

(you can also name it "import"by passing the argument with a dict)

(您也可以"import"通过使用 a 传递参数来命名它dict

kwargs = { 'import' : importlib.import_module }
my_template.render( **kwargs )

then in the jinja-template, you can import any module:

然后在 jinja 模板中,您可以导入任何模块:

{% set time = imp0rt( 'time' ) %}
{{ time.time() }}