Nested Layouts

Nested layouts allow you to compose templates in multiple layers. Each layer can define its own structure and pass control to child templates.

---

1. Basic Concept


$admin = $tpl->slot('content','admin');
$admin->slot('content','dashboard');

Hierarchy:

base → admin → dashboard
---

2. Layout Structure Example

base.html

<div class="container">
  <div data-slot="content"></div>
</div>

admin.html

<div class="admin-panel">
  <h2>Admin</h2>
  <div data-slot="content"></div>
</div>

dashboard.html

<div>
  <h3 id="title"></h3>
</div>
---

3. Data Binding in Nested Templates


$admin = $tpl->slot('content','admin');
$dashboard = $admin->slot('content','dashboard');

$dashboard->pick('title')->content('Dashboard');
Each level has its own scope but can inherit from parent.
---

4. Assignment Inheritance


$tpl->pick('appName')->content('My App');

$admin = $tpl->slot('content','admin');
$dashboard = $admin->slot('content','dashboard');

👉 dashboard can still access appName

---

5. Multiple Nested Levels


$level1 = $tpl->slot('content','layout1');
$level2 = $level1->slot('content','layout2');
$level3 = $level2->slot('content','page');

👉 Unlimited nesting supported

---

6. Nested + Includes


$admin = $tpl->slot('content','admin');

$header = $admin->include('#header','header');
$header->pick('user')->content('Tamil');
---

7. Nested + Loops


$order = $tpl->addLoopItem('orders');

$child = $order->slot('content','order-item');
$child->pick('name')->content('Pizza');
Nested layouts can be used inside loops.
---

8. Nested + Data Files


// data.php
$admin = $this->slot('content','admin');
$admin->slot('content','dashboard');
---

9. Execution Flow (Important)

Rendering happens in this order:

  1. Base template loads
  2. Slot replaced with child template
  3. Child template renders
  4. Process repeats recursively
---

10. Common Mistakes

❌ Slot name mismatch
❌ Missing data-slot in child layout
❌ Overwriting same slot multiple times
❌ Calling slot on wrong template instance
---

11. Rules

---

12. Best Practices

---

13. Real-World Example (Restaurant App)


$app = $tpl->slot('content','app-layout');

$orders = $app->slot('content','orders-page');

$orders->pick('table')->content('Table 5');
---

14. Performance Notes

---
Nested layouts enable modular UI composition similar to modern frameworks.