Learning Jinja2: Part 4: Template Inheritance and Blocks

Learning Jinja2

Jinja2 is a template engine for Python featuring an optional sandbox environment.


Template Inheritance and Blocks

Built into Jinja2 is functionality for building a ‘base’ template file and building off of it. This is known in Jinja2 functionality as “extending” a file.

Files extend when they use the keyword block to define a section. The keyword endblock closes out a section. In the base file, these sections define areas where other, “child” files can add content.

base.html

Extending

Files that extend from other files use the keyword extend and the name of the parent file.

In the child file, any use of the keywords block and endblock define areas that override the parent.

Rendered child file

Play with the example on Repl.it!