File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ # 🧱 Template Inheritance in Flask (Jinja2)
2+
3+ - Used in ** HTML** , not Python → works with ` .html ` templates
4+ - Helps avoid repeating code like headers/footers
5+
6+ ---
7+
8+ ## 🗂️ Base Template (` layout.html ` )
9+
10+ ``` html
11+ <header >...</header >
12+ {% block body %}{% endblock %}
13+ <footer >...</footer >
14+ ```
15+
16+ - ` layout.html ` holds common structure (header, footer)
17+ - ` {% block body %} ` defines a replaceable section
18+
19+ ---
20+
21+ ## 📄 Child Template (e.g. ` index.html ` )
22+
23+ ``` html
24+ {% extends "layout.html" %}
25+ {% block body %}
26+ <!-- Page-specific content here -->
27+ {% endblock %}
28+ ```
29+
30+ - ` {% extends %} ` loads ` layout.html `
31+ - Content inside ` {% block body %} ` replaces the block in ` layout.html `
32+
33+ ---
34+
35+ ## 🧠 How It Works
36+
37+ - ` extends ` pulls in layout
38+ - ` block ` replaces parts of layout
39+ - Result: header + your content + footer
40+
41+ ---
42+
43+ ## ✅ Summary
44+
45+ - Shared layout → ` layout.html `
46+ - Unique content → inside ` {% block %} ` in child templates
47+ - Keeps templates clean & DRY
You can’t perform that action at this time.
0 commit comments