TEMPLATE INHERITANCE FLOW DIAGRAM
====================================
┌─────────────────────────────┐
│ START: Render page.tpl │
└──────────────┬──────────────┘
│
▼
┌─────────────────────────────┐
│ Load page.tpl │
└──────────────┬──────────────┘
│
▼
┌─────────────────┐
│ Has extends │
│ directive? │
└────┬───────┬────┘
│ │
Yes │ │ No
(extends layout) │ │
▼ ▼
┌──────────────┐ │
│ Load layout │ │
│ .tpl │ │
└──────┬───────┘ │
│ │
▼ │
┌─────────────────┐ │
│ Has extends │ │
│ directive? │ │
└────┬───────┬────┘ │
│ │ │
Yes │ │ No │
(extends base) │ │ │
▼ │ │
┌──────────────┐ │ │
│Load base.tpl │ │ │
└──────┬───────┘ │ │
│ │ │
▼ │ │
┌─────────────────┐ │ │
│ Has extends │ │ │
│ directive? │ │ │
└────┬───────┬────┘ │ │
│ │ │ │
Yes │ │ No │ │
│ │ │ │
▼ ▼ ▼ ▼
┌──────────────────────────────┐
│ Chain complete: │
│ [base.tpl, layout.tpl, │
│ page.tpl] │
└────────────┬─────────────────┘
│
▼
┌──────────────────────────────┐
│ Reverse chain order: │
│ base.tpl → layout.tpl → │
│ page.tpl │
└────────────┬─────────────────┘
│
▼
┌──────────────────────────────┐
│ Extract blocks from each │
│ template in chain │
└────────────┬─────────────────┘
│
▼
┌──────────────────────────────────────────┐
│ STEP 1: Process base.tpl │
│ ──────────────────────────── │
│ {% block header %}Default{% endblock %} │
│ {% block content %}...{% endblock %} │
│ {% block footer %}...{% endblock %} │
│ │
│ Store blocks: │
│ blocks['header'] = "Default" │
│ blocks['content'] = "..." │
│ blocks['footer'] = "..." │
└─────────────────┬────────────────────────┘
│
▼
┌──────────────────────────────────────────┐
│ STEP 2: Process layout.tpl │
│ ──────────────────────────── │
│ {% block header %} │
│ {{ super() }} - My Site │
│ {% endblock %} │
│ │
│ Override block: │
│ Replace {{ super() }} with │
│ previous content "Default" │
│ blocks['header'] = │
│ "Default - My Site" │
└─────────────────┬────────────────────────┘
│
▼
┌──────────────────────────────────────────┐
│ STEP 3: Process page.tpl │
│ ──────────────────────────── │
│ {% block content %} │
│ <h1>Welcome</h1> │
│ <p>Page content</p> │
│ {% endblock %} │
│ │
│ Override block: │
│ blocks['content'] = │
│ "<h1>Welcome</h1>..." │
└─────────────────┬────────────────────────┘
│
▼
┌──────────────────────────────────────────┐
│ MERGE: Replace block tags in │
│ base.tpl with final block content │
│ ──────────────────────────── │
│ Before: │
│ {% block header %}...{% endblock %} │
│ After: │
│ Default - My Site │
│ │
│ Result: Fully merged template with │
│ all inheritance resolved │
└─────────────────┬────────────────────────┘
│
▼
┌──────────────────────────────┐
│ Merged template ready │
└────────────┬─────────────────┘
│
▼
┌───────────────┐
│ Cache │
│ enabled? │
└───┬───────┬───┘
│ │
Yes │ │ No
│ │
▼ ▼
┌─────────────────┐ │
│ COMPILE TO PHP │ │
│ ─────────────── │ │
│ • Process {% %} │ │
│ • Process {{ }} │ │
│ • Inline │ │
│ includes │ │
│ • Generate │ │
│ pure PHP │ │
└────────┬────────┘ │
│ │
▼ │
┌────────────────┐ │
│ Save to cache │ │
│ MD5(name).php │ │
└────────┬───────┘ │
│ │
▼ ▼
┌──────────────────────────────┐
│ Execute compiled PHP or │
│ render merged template │
└────────────┬─────────────────┘
│
▼
┌──────────────────────────────┐
│ OUTPUT HTML │
└──────────────────────────────┘
KEY CONCEPTS:
═════════════
1. LOADING CHAIN
─────────────
Templates are loaded following {% extends %} directives
until a template with no extends is found.
Example chain: page.tpl → layout.tpl → base.tpl
2. REVERSAL
────────
The chain is reversed to process from parent to child.
This ensures child blocks override parent blocks.
Processing order: base.tpl → layout.tpl → page.tpl
3. BLOCK OVERRIDE
──────────────
Each child template can override parent blocks.
Use {{ super() }} to include parent block content.
Parent: {% block title %}Default{% endblock %}
Child: {% block title %}{{ super() }} - Page{% endblock %}
Result: "Default - Page"
4. COMPILATION
───────────
The merged template is compiled to pure PHP code:
• {% if %} becomes <?php if (...) { ?>
• {{ var }} becomes <?php echo htmlspecialchars(...) ?>
• {% include %} is resolved and inlined
Result: Fast, executable PHP with no template parsing
5. CACHING
────────
Compiled PHP is cached with MD5 hash filename.
Cache invalidates when any template in chain is modified.
Subsequent renders execute cached PHP directly.