Multiple level template inheritance in Jinja2? Multiple level template inheritance in Jinja2? python python

Multiple level template inheritance in Jinja2?


One of the best way to achieve multiple level of templating using jinja2 is to use 'include'let say you have 'base_layout.html' as your base template

<!DOCTYPE html><title>Base Layout</title><div>  <h1>Base</h1>  .... // write your code here  {% block body %}{% endblock %}</div>

and then you want to have 'child_layout.html' that extends 'base_layout.

{% include "base_layout.html" %}  <div>  ... // write your code here  </div>{% block body %}{% endblock %}

and now your page can just extends 'child_layout.html' and it will have both base_layout.html and child_layout.html

{% extends "child_layout.html" %}{% block body %}  ...// write your code here{% endblock %}


The way the documentation worded it, it seemed like it didn't support inheritance (n) levels deep.

Unlike Python Jinja does not support multiple inheritance. So you can only have one extends tag called per rendering.

I didn't know it was just a rule saying 1 extends per template.... I now know, with some help from the jinja irc channel.


Try this, this work for me thanks to @Ixm answer.

base.html

<html xmlns="http://www.w3.org/1999/xhtml">    <body>      {% block content %}{% endblock %}    </body></html>

content.html

{% extends "base.html" %}{% block content %}<table>  <tr>  {% include "footer.html" %}  </tr></table>{% endblock %}

footer.html

{% block footer %} <td> test</td>{% endblock %}

and call with

env = Environment(loader=FileSystemLoader(os.path.join(path, "Layouts")))template = env.get_template('content.html')html = template.render()print html