Discover how to stop repeating yourself and make website updates a breeze!
Why Block definitions and overriding in Flask? - Purpose & Use Cases
Imagine building a website where every page shares the same header and footer, but the main content changes. You try copying and pasting the full HTML for each page, changing only the middle part.
This manual copying leads to lots of repeated code. If you want to update the header, you must change every page separately. It's slow, error-prone, and hard to keep consistent.
Block definitions and overriding let you create a base template with placeholders (blocks). Each page can fill or replace these blocks, so shared parts stay in one place and only unique parts change.
<html><body><header>Site header</header><main>Page 1 content</main><footer>Site footer</footer></body></html> <html><body><header>Site header</header><main>Page 2 content</main><footer>Site footer</footer></body></html>
{% extends 'base.html' %}
{% block content %}Page 1 content{% endblock %}
<!-- base.html -->
{% block content %}{% endblock %}This makes your website easy to maintain and update, letting you focus on unique page content while keeping the shared layout consistent.
Think of a blog where every post has the same look but different text. Using blocks, you write the header and footer once, then just override the post content for each article.
Blocks let you define placeholders in templates.
Overriding blocks lets pages customize parts without repeating all code.
This keeps your site consistent and easy to update.