Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Jinja2 templating language have the concept of 'here' (current directory)?

Tags:

python

jinja2

Does Jinja2 support template-relative paths e.g. %(here)s/other/template.html, to include other templates relative to the current template's place in the filesystem?

like image 546
joeforker Avatar asked Feb 01 '10 21:02

joeforker


People also ask

What is the main purpose of Jinja2 template usage with Ansible?

Jinja2 templates are simple template files that store variables that can change from time to time. When Playbooks are executed, these variables get replaced by actual values defined in Ansible Playbooks. This way, templating offers an efficient and flexible solution to create or alter configuration file with ease.

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.


1 Answers

I do not believe so. Typically you include or extend other templates by specifying their paths relative to the root of whatever template loader and environment you're using.

So let's say your templates are all in /path/to/templates and you've set up Jinja like so:

import jinja2 template_dir = '/path/to/templates' loader = jinja2.FileSystemLoader(template_dir) environment = jinja2.Environment(loader=loader) 

Now, if you'd like to include /path/to/templates/includes/sidebar.html in the /path/to/templates/index.html template, you'd write the following in your index.html:

{% include 'includes/sidebar.html' %} 

and Jinja would figure out how to find it.

like image 192
Will McCutchen Avatar answered Sep 30 '22 00:09

Will McCutchen