Before reaching for Twig or Blade, it’s worth understanding that PHP itself was originally designed as a templating language — building a minimal one yourself clarifies what a “real” templating engine adds on top.
class SimpleTemplate {
public function render(string $templatePath, array $data = []): string {
extract($data); // makes array keys available as variables
ob_start();
include $templatePath;
return ob_get_clean();
}
}
<h1>Welcome, <?= htmlspecialchars($name) ?>!</h1>
<ul>
<?php foreach ($items as $item): ?>
<li><?= htmlspecialchars($item) ?></li>
<?php endforeach; ?>
</ul>
What Twig and Blade add on top of this is automatic output escaping by default (reducing the risk of forgetting htmlspecialchars()), template inheritance, and a restricted syntax that prevents templates from running arbitrary PHP — genuinely valuable trade-offs once a project grows past a handful of views.