COLOR LEGEND:
█ PURPLE =
Inheritance (extends)
█ CYAN = Blocks
(define/override)
█ YELLOW =
Includes (partials)
█ ORANGE = Loops
(for)
█ L.BLUE =
Variables (output)
█ PURPLE =
Conditionals (if/else)
█ CORAL =
Cache/Compile
█ B.BLUE = Final
Output
┌──────────────────┐
│ INPUT DATA │
│ ──────────── │
│ $ctx = [ │
│ 'user' => [ │
│ 'name'=>'A' │
│ ], │
│ 'items'=>[...] │
│ ] │
└────────┬─────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ STEP 1: LOAD TEMPLATE CHAIN │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌────────────────────────────┐ ┌───────────────────────────┐ ┌──────────────────────────┐
│ page.tpl (Child) │ │ layout.tpl (Parent) │ │ base.tpl (Root) │
│ ──────────────────────── │ │ ─────────────────────── │ │ ────────────────────── │
│ {% extends "layout.tpl" %} │────> │ {% extends "base.tpl" %} │─────>│ <!doctype html> │
│ │ │ │ │ <html> │
│ {% block title %} │ │ {% block header %} │ │ {% block title %} │
│ My Page │ │ <h1>Site</h1> │ │ Default │
│ {% endblock %} │ │ {% endblock %} │ │ {% endblock %} │
│ │ │ │ │ {% block header %} │
│ {% block content %} │ │ {% block content %} │ │ {% endblock %} │
│ Hello {{ user.name }} │ │ {{ super() }} │ │ {% block content %} │
│ {% for item in items %} │ │ <footer>Extra</footer> │ │ {% endblock %} │
│ {% include "item.tpl" %} │ │ {% endblock %} │ │ </html> │
│ {% endfor %} │ │ │ │ │
│ {% endblock %} │ │ │ │ │
└────────────────────────────┘ └───────────────────────────┘ └──────────────────────────┘
│ │ │
└─────────────────────────────┴────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ STEP 2: REVERSE & MERGE BLOCKS │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ BLOCK TRACKING TABLE │
│ ═══════════════════════ │
│ │
│ Block Name │ After base.tpl │ After layout.tpl │ After page.tpl (FINAL) │
│ ───────────────────────────────────────────────────────────────────────────────────────────────────────│
│ title │ "Default" │ "Default" │ "My Page" │
│ header │ "" │ "<h1>Site</h1>" │ "<h1>Site</h1>" │
│ content │ "" │ "" + "<footer>Extra..." │ "Hello {{user.name}}..." │
│ │
│ {{ super() }} in layout.tpl replaced with previous content from base.tpl │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────┘
│
▼
<!doctype html>
<html>
<title>My Page</title>
<h1>Site</h1>
Hello {{ user.name }}
{% for item in items %}
{% include "partials/item.tpl" with entry=item %}
{% endfor %}
<footer>Extra</footer>
</html>
│
▼
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ STEP 3: RESOLVE INCLUDES (Compile-time) │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌────────────────────────────────────┐
│ partials/item.tpl (Include) │
│ ──────────────────────────── │
│ <li>{{ entry }}</li> │
└────────────────────────────────────┘
│
▼
<?php $__saved_ctx = $ctx; $ctx['entry'] = $ctx['item']; ?>
<li><?php echo htmlspecialchars($ctx['entry']); ?></li>
<?php $ctx = $__saved_ctx; ?>
│
▼
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ STEP 4: COMPILE TO PHP │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────┘
{{ user.name }} ══▶ <?php $__v = star_ctx_get($ctx, 'user.name');
if ($__v !== NULL) { echo star_e($__v); } ?>
{% for item in items %} ══▶ <?php $__list = star_ctx_get($ctx, 'items');
if (is_array($__list)) {
foreach ($__list as $__k => $__v) {
$ctx['item'] = $__v;
$ctx['item_key'] = $__k; ?>
{% endfor %} ══▶ <?php } } ?>
{% if user.age >= 18 %} ══▶ <?php if (star_eval_condition('user.age >= 18', $ctx)) { ?>
Adult Adult
{% else %} ══▶ <?php } else { ?>
Minor Minor
{% endif %} ══▶ <?php } ?>
{{ raw(html) }} ══▶ <?php $__v = star_ctx_get($ctx, 'html');
if ($__v !== NULL) {
echo (star_is_raw($__v)) ? $__v['html'] : (string)$__v;
} ?>
│
▼
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ STEP 5: CACHE COMPILED PHP │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────┘
cache/74a7dbf2831ed066620d84d1a43dd346.php
──────────────────────────────────────────
<!doctype html>
<html>
<title>My Page</title>
<h1>Site</h1>
Hello <?php $__v = star_ctx_get($ctx, 'user.name');
if ($__v !== NULL) { echo star_e($__v); } ?>
<?php $__list = star_ctx_get($ctx, 'items');
if (is_array($__list)) { foreach ($__list as $__k => $__v) {
$ctx['item'] = $__v; $ctx['item_key'] = $__k; ?>
<?php $__saved_ctx = $ctx; $ctx['entry'] = $ctx['item']; ?>
<li><?php echo htmlspecialchars($ctx['entry']); ?></li>
<?php $ctx = $__saved_ctx; ?>
<?php } } ?>
<footer>Extra</footer>
</html>
│
▼
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ STEP 6: EXECUTE & OUTPUT │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Context: $ctx = ['user' => ['name' => 'Alice'], 'items' => ['apple', 'banana']]
│
▼
│
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ FINAL HTML OUTPUT │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────┘
<!doctype html>
<html>
<title>My Page</title>
<h1>Site</h1>
Hello Alice
<li>apple</li>
<li>banana</li>
<footer>Extra</footer>
</html>
FIRST RENDER (Cold Cache):
1. Load template chain
2. Merge blocks
3. Resolve includes
4. Compile to PHP
5. Save to cache
6. Execute
SUBSEQUENT RENDERS (Hot Cache):
1. Check cache validity
2. Execute cached PHP
CACHE INVALIDATION TRIGGERS:
• base.tpl modified
• layout.tpl modified
• page.tpl modified
• Any included partial modified