如何获取Jinja2模板中当前模板的文件名? [英] How to get file name of current template inside Jinja2 template?

查看:174
本文介绍了如何获取Jinja2模板中当前模板的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

虽然没有文档,但是 {{self._TemplateReference__context.name}} 会给出模板名称。并且在 self._TemplateReference__context 之后可以使用一些间隔属性。

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.

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

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.

这篇关于如何获取Jinja2模板中当前模板的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆