Template Blocks
A block is a named placeholder region that child templates can override.
flowchart LR
subgraph "base.html"
T["{% block title %}"]
H["{% block head %}"]
C["{% block content %}"]
S["{% block scripts %}"]
end
subgraph "article.html"
T2["title -> the article headline"]
C2["content -> the article body"]
end
T -.-> T2
C -.-> C2
H -.->|"not overridden"| HD["base default is kept"]
S -.->|"not overridden"| SD["base default is kept"]
Defining blocks
Section titled “Defining blocks”<!-- base.html -->
{% block content %}{% endblock %}Overriding blocks
Section titled “Overriding blocks”<!-- page.html -->
{% extends "base.html" %}
{% block content %}
<h1>Page content</h1>
{% endblock %}Using multiple blocks
Section titled “Using multiple blocks”Base templates often have blocks like:
titlecontentscripts
{% block scripts %}{% endblock %}Child template:
{% block scripts %}
<script src="{{ url_for('static', filename='app.js') }}"></script>
{% endblock %}Extending a block (super)
Section titled “Extending a block (super)”If you want to add to a parent block rather than replace it:
{% block scripts %}
{{ super() }}
<script src="{{ url_for('static', filename='extra.js') }}"></script>
{% endblock %}That’s useful for base scripts shared across pages.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading