Skip to content

Commit 0e662c6

Browse files
committed
Template inheritance notes
1 parent ac7dff9 commit 0e662c6

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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

0 commit comments

Comments
 (0)