Location of the Jinja Environment directory


the Jinja Template engine always requires an environment to draw the templates from. Thus an exemplified python module, whose purpose is rendering templates would start like this:

 from Jinja2 import Environment, FileSystemLoader
 env = Environment( loader = FileSystemLoader('templates') )
 myTemplate = env.get_template('template1.html')
 print(myTemplate.render()) ...

Here the FileSystemLoader loads a directory ‘templates’ relative to the location of the calling module. So I would deposit every html template (like template1.html, template2.html) into that directory. So this directory forms a crucial part of the Jinja environment.

If I would now import a Jinja Macro from template1.html into template2.html the import statement knows, where to find template1.html, since the FileSystemLoader defined the location. Namely ‘templates’. So template2.html would start with:
{% from template1.html import macro1 %}, where macro1 is a Jinja Macro defined in template1.html.

So now, I want to know, where this directory is located within the Indico project. I see that many templates, that import macros refer to indico/indico/web/templates, but some Modules like, e.g. groups ( in indico/indico/modules) contain html templates refering to the module groups, like e.g. /indico/indico/modules/groups/templates/groupd_details.html



Hi, please explain what you are trying to do and we can give you some tips. We use some custom logic to avoid duplicating e.g. module names inside the module(blueprint)-specific template folders.

Hi ThiefMaster,

it is a question of general interest. I am learning Jinja at the moment in order to better understand the templating infrastructure of Indico. I recognized today, that Indico templates make heavy use of macros. As far as I know, a macro is imported like e.g. in

{% from 'message_box.html' import message_box %}, where message_box.html contains the macro message_box.

Now I want to know from where Jinja knows, where the template message_box.html is located.

The default template folder indico/web/templates/ is set here:

All the modules have their own blueprint, e.g. here:

By default, the template folder that belongs to the blueprint would have priority over core templates, and share the same namespace (this is a design decision in Flask that some people like and some don’t). However, in our case it’s not convenient and usually we’d have to put the templates for that blueprint inside templates/announcement/whatever if we wanted to render them as 'announcement/whatever'. But repeating the module name inside the template folder is somewhat dumb, so I added some code that adds a virtual prefix for all templates in the blueprint’s template folder:

Hi Thiefmaster,

that was very clarifying. Thank you for your detailed response.