How to get file name of current template inside Jinja2 template?

html, jinja2, python

Solution

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.

Problem

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?

Original source