Lesson 40 โข Advanced
Template Engines ๐จ
Separate logic from presentation with Twig, use template inheritance for DRY layouts, and build reusable components with filters and blocks.
What You'll Learn in This Lesson
- โข Why mixing PHP and HTML creates unmaintainable code
- โข Use Twig syntax: output, logic, loops, and filters
- โข Build DRY layouts with template inheritance and blocks
- โข Auto-escape output to prevent XSS attacks
- โข Create reusable partials with include and macros
Twig Basics & Filters
Twig separates your HTML from PHP logic with a clean syntax: {{ variable }} for output, {% if %} for logic, and {{ name|upper }} for filters. Unlike raw PHP, Twig auto-escapes all output โ preventing XSS attacks without manually calling htmlspecialchars() everywhere.
Try It: Twig Template Engine
Register templates, apply filters, and render with context variables
// Twig Template Engine: Separating Logic from Presentation
console.log("=== Why Template Engines? ===");
console.log();
console.log(" โ Mixing PHP + HTML (spaghetti code):");
console.log(' <?php foreach ($users as $user): ?>');
console.log(' <div class="<?= $user["active"] ? "green" : "red" ?>">');
console.log(' <?= htmlspecialchars($user["name"]) ?>');
console.log(" </div>");
console.log(" <?php endforeach; ?>");
console.log();
console.log(" โ
Twig (clean separati
...Template Inheritance & Layouts
Instead of including header and footer in every page, define a base layout with replaceable blocks. Child templates extend the base and override only the blocks they need. This ensures every page has consistent navigation, styling, and structure while keeping individual pages focused on their unique content.
Try It: Layout Inheritance
Define a base layout with blocks and render multiple pages
// Template Inheritance & Layouts
console.log("=== Template Inheritance: DRY Layouts ===");
console.log();
console.log(" The Problem: Every page repeats <html>, <head>, <nav>, <footer>");
console.log(" The Solution: One base layout, child templates override blocks");
console.log();
class LayoutEngine {
constructor() { this.layouts = {}; }
defineLayout(name, blocks) {
this.layouts[name] = blocks;
console.log(" ๐ Layout defined: " + name);
console.log(" Blocks: " + Object
...โ ๏ธ Common Mistakes
{{ variable|raw }} carelessly โ The raw filter disables auto-escaping. Only use it for trusted HTML content you generated yourself, never for user input.new Environment($loader, ['cache' => '/tmp/twig_cache']) โ it compiles templates to PHP classes for near-zero overhead.๐ Quick Reference โ Template Engines
| Syntax | Purpose |
|---|---|
| {{ var }} | Output variable (auto-escaped) |
| {% if %} | Control flow (if, for, block) |
| {# comment #} | Template comment (not rendered) |
| extends | Inherit from parent template |
| include | Insert another template (partial) |
๐ Lesson Complete!
You can now build clean, maintainable PHP views with Twig! Next, learn to monitor APIs, log usage, and build audit trails.
Sign up for free to track which lessons you've completed and get learning reminders.