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!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.