liveBook · Manning

Template inheritance

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

Template inheritance is a templating pattern in which a child template extends a parent template and overrides or fills named sections.

A banner, footer, and HTML boilerplate often appear on every page. Copying them into each template makes changes harder to maintain. Template inheritance follows the “don’t repeat yourself” principle: keep shared structure in a parent template and put page-specific content in child templates. section 6.4.4

A shared layout and a home page

A base.html template defines the page structure and named blocks such as head, styles, content, and footer. The home page starts with `{% extends "base.html" %}` and fills the content block with the current time, visit count, and available banner colors. Jinja2 renders the result as a complete HTML page. section 6.4.4

Keep presentation and interactivity files in Flask’s static directory, alongside the templates directory; CSS and JS subdirectories help organize them. Inheritance handles shared HTML structure, while those files supply styling and behavior. section 6.4.4

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Shows a parent layout with named blocks and a child that extends it; does not explore inheritance edge cases.
Examples
Uses a home page with a shared banner and footer, plus a content block showing time, visits, and banner colors.
What it assumes
Assumes familiarity with HTML templates and a Flask application; introduces the static directory arrangement explicitly.
What you can do after
Apply it via Jinja2 to separate a shared page layout from page-specific content.
How it connects
Connects template inheritance to DRY, Python class inheritance, Flask static files, CSS, and JavaScript.

Also explained in

Compare how 3 books explain template inheritance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.