如何在Jinja2模板中获取当前模板的文件名?

时间:2022-03-26 19:35:30

Say I use return render_template('index.html', users=users). Is it possible to get the filename inside a template without explicit sending of it inside the view?

假设我使用return render_template('index.html',users = users)。是否可以在模板中获取文件名而无需在视图中显式发送?

2 个解决方案

#1


9  

Although undocumented, {{ self._TemplateReference__context.name }} will give the template name. And there are a number of interresting attributes that you can use after self._TemplateReference__context.

虽然没有文档,{{self._TemplateReference__context.name}}将给出模板名称。在self._TemplateReference__context之后,您可以使用许多有趣的属性。

You could, for example, add this to your topmost base template:

例如,您可以将其添加到最顶层的基本模板中:

        <meta name="template" content="{{ self._TemplateReference__context.name }}">

So that looking at a page source will help you quickly find the relevant template file. If you don't want to expose this kind of information, make it conditional to testing environment.

因此,查看页面源可帮助您快速找到相关的模板文件。如果您不想公开此类信息,请将其作为测试环境的条件。

#2


6  

If all you need is the basename, you can use {{ self }} which will return a repr string containing the basename of the template, e.g., <TemplateReference 'view.html'>. You could parse this with a filter, e.g., {{ self | quoted }}

如果您只需要基本名称,则可以使用{{self}},它将返回包含模板基本名称的repr字符串,例如 。您可以使用过滤器对其进行解析,例如{{self |引用}} 'view.html'>

@app.template_filter('quoted')
def quoted(s):
    l = re.findall('\'([^\']*)\'', str(s))
    if l:
        return l[0]
    return None

If you need the full path, e.g., '/full/path/to/view.html' you may want to subclass Template.

如果您需要完整路径,例如'/full/path/to/view.html',您可能想要继承模板。

#1


9  

Although undocumented, {{ self._TemplateReference__context.name }} will give the template name. And there are a number of interresting attributes that you can use after self._TemplateReference__context.

虽然没有文档,{{self._TemplateReference__context.name}}将给出模板名称。在self._TemplateReference__context之后,您可以使用许多有趣的属性。

You could, for example, add this to your topmost base template:

例如,您可以将其添加到最顶层的基本模板中:

        <meta name="template" content="{{ self._TemplateReference__context.name }}">

So that looking at a page source will help you quickly find the relevant template file. If you don't want to expose this kind of information, make it conditional to testing environment.

因此,查看页面源可帮助您快速找到相关的模板文件。如果您不想公开此类信息,请将其作为测试环境的条件。

#2


6  

If all you need is the basename, you can use {{ self }} which will return a repr string containing the basename of the template, e.g., <TemplateReference 'view.html'>. You could parse this with a filter, e.g., {{ self | quoted }}

如果您只需要基本名称,则可以使用{{self}},它将返回包含模板基本名称的repr字符串,例如 。您可以使用过滤器对其进行解析,例如{{self |引用}} 'view.html'>

@app.template_filter('quoted')
def quoted(s):
    l = re.findall('\'([^\']*)\'', str(s))
    if l:
        return l[0]
    return None

If you need the full path, e.g., '/full/path/to/view.html' you may want to subclass Template.

如果您需要完整路径,例如'/full/path/to/view.html',您可能想要继承模板。