Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 973
Sergei Basharov Avatar asked Jun 22 '12 13:06

Sergei Basharov


People also ask

What is the difference between Jinja and Jinja2?

Jinja, also commonly referred to as "Jinja2" to specify the newest release version, is a Python template engine used to create HTML, XML or other markup formats that are returned to the user via an HTTP response.

What is Autoescape in Jinja2?

B701: Test for not auto escaping in jinja2When autoescaping is enabled, Jinja2 will filter input strings to escape any HTML content submitted via template variables. Without escaping HTML input the application becomes vulnerable to Cross Site Scripting (XSS) attacks. Unfortunately, autoescaping is False by default.


1 Answers

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.

like image 50
patb Avatar answered Sep 24 '22 17:09

patb