MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Template Inheritance & Layout Composition0%
Study Materials
FlaskProgress 0%
1.1What is Flask & WSGI Architecture1.2Virtual Environments & First Flask Application1.3Flask CLI & Environment Configuration
3.1Jinja2 Syntax, Variables & Custom Filters3.2Control Structures, Loops & Conditionals3.3Template Inheritance & Layout Composition3.4Jinja2 Macros & Reusable UI Components
HomeTutorialsFlaskTemplate Inheritance & Layout Composition
Jinja2 Macros & Reusable UI Components

Template Inheritance & Layout Composition

Beginner12 min read•Updated: 2026-09-16•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

Copying and pasting navigation bars, footers, head meta tags, and stylesheet imports across dozens of HTML pages is a recipe for maintenance nightmares. Template inheritance is Jinja2's most powerful feature, enabling you to build a master skeleton template containing common elements while child templates override specific content blocks.


1. The Master Skeleton (templates/base.html)

The base template defines the universal structure of your website and marks customizable areas using {% block <name> %}{% endblock %}:

1<!-- templates/base.html -->
2<!DOCTYPE html>
3<html lang="en">
4<head>
5 <meta charset="UTF-8">
6 <meta name="viewport" content="width=device-width, initial-scale=1.0">
7 <title>{% block title %}MSK Institute{% endblock %}</title>
8
9 <!-- Universal CSS Stylesheets -->
10 <link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
11
12 <!-- Child templates can inject custom page-specific styles here -->
13 {% block extra_css %}{% endblock %}
14</head>
15<body class="bg-gray-50 text-gray-900 flex flex-col min-h-screen">
16 
17 <!-- Global Navigation Header -->
18 <header class="bg-indigo-900 text-white shadow-md">
19 <div class="max-w-7xl mx-auto px-4 py-4 flex justify-between items-center">
20 <a href="/" class="text-2xl font-bold tracking-tight">MSK Institute</a>
21 <nav class="space-x-6">
22 <a href="/" class="hover:text-indigo-200">Home</a>
23 <a href="/courses" class="hover:text-indigo-200">Courses</a>
24 <a href="/contact" class="hover:text-indigo-200">Contact</a>
25 </nav>
26 </div>
27 </header>
28 
29 <!-- Main Content Area -->
30 <main class="flex-grow max-w-7xl mx-auto px-4 py-8 w-full">
31 {% block content %}
32 <!-- Default content replaced by child templates -->
33 {% endblock %}
34 </main>
35 
36 <!-- Global Footer -->
37 <footer class="bg-gray-900 text-gray-400 py-6 text-center text-sm">
38 <p>&copy; 2026 MSK Institute of Technology. All rights reserved.</p>
39 </footer>
40 
41 <!-- Universal Scripts -->
42 <script src="{{ url_for('static', filename='js/app.js') }}"></script>
43 {% block extra_js %}{% endblock %}
44</body>
45</html>

2. Child Templates ({% extends %})

Child templates declare which base template they inherit from using {% extends "base.html" %}. This must be the very first tag in the child template file!

1<!-- templates/courses.html -->
2{% extends "base.html" %}
3 
4{% block title %}Explore Engineering Courses - MSK Institute{% endblock %}
5 
6{% block extra_css %}
7<link rel="stylesheet" href="{{ url_for('static', filename='css/course-cards.css') }}">
8{% endblock %}
9 
10{% block content %}
11<section class="space-y-6">
12 <h1 class="text-3xl font-extrabold text-indigo-950">Professional Certification Courses</h1>
13 <p class="text-gray-600">Choose from industry-aligned full-stack and cloud tracks.</p>
14 
15 <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
16 {% for course in courses %}
17 <div class="card p-6 bg-white rounded-xl shadow-sm border border-gray-100">
18 <h2 class="text-xl font-bold">{{ course.title }}</h2>
19 <p class="text-sm text-gray-500 mt-2">{{ course.description }}</p>
20 <a href="/courses/{{ course.slug }}" class="mt-4 inline-block text-indigo-600 font-semibold">
21 Enroll Now &rarr;
22 </a>
23 </div>
24 {% endfor %}
25 </div>
26</section>
27{% endblock %}

3. Preserving Parent Content with super()

If a child template wants to add content to a block without completely overwriting what the parent template defined, call {{ super() }}:

1<!-- Child template appending a title -->
2{% block title %}
3 {{ super() }} | Courses Catalog
4{% endblock %}
5<!-- If parent title was "MSK Institute", output becomes: "MSK Institute | Courses Catalog" -->

4. Modular Partials with {% include %}

While {% extends %} provides top-down vertical page scaffolding, {% include %} allows horizontal inclusion of modular template snippets (e.g. newsletter banners, navigation bars, modals):

1<!-- Include a reusable newsletter signup form -->
2<div class="sidebar">
3 {% include "partials/_newsletter_box.html" %}
4 {% include "partials/_social_links.html" %}
5</div>

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What rule must be strictly followed when using {% extends %} in a child template?
Question 2
How does a child template keep and append to the content defined in the parent block instead of replacing it?
Question 3
What is the key difference between {% extends %} and {% include %}?
Question 4
What happens if a child template does not define a block that exists in the parent template?
Question 5
Can child templates define multiple blocks (e.g., title, content, extra_js) within the same file?
Next Lesson

Jinja2 Macros & Reusable UI Components

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Control Structures, Loops & ConditionalsJinja2 Macros & Reusable UI Components

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Flask with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext