Twig for PHP: Complete Guide to the Symfony Template Engine
\n\nTwig is a modern, fast, and secure template engine for the PHP language, developed by the Symfony team. It provides developers with a powerful yet intuitive syntax for separating presentation logic (templates) from application business logic. Twig solves a key problem in web development: mixing PHP code with HTML, which leads to "spaghetti code" and reduces project readability and maintainability.
\n\nThe main goal of Twig is to make templates clean, secure, and easy to read. Unlike raw PHP, Twig restricts developers from executing arbitrary code inside templates, which prevents common vulnerabilities such as XSS attacks (Cross-Site Scripting) through automatic output escaping. Instead of complex constructs like <?php foreach(...): ?>, you use concise {% for item in items %}, and instead of <?= $var ?>, you use {{ var }}. This makes templates accessible to layout designers and frontend developers unfamiliar with PHP.
Twig is the de facto standard for the Symfony framework, but it also integrates well with Laravel (via the twigbridge package), Drupal, WordPress, and can be used as a standalone library in any PHP project. Thanks to features such as template inheritance, macros, sandboxing, and automatic escaping, Twig is one of the most popular template engines in the PHP ecosystem.
Installation
\nTwig installation is done via the Composer dependency manager. Make sure Composer is installed globally.
\ncomposer require twig/twig:^3.0\nAfter executing the command, a vendor/ folder and a composer.json file with the added dependency will appear in your project. Use the standard vendor/autoload.php file for class autoloading.
Quick Start: Minimal Working Example
\nLet's create a simple template and display it. Project structure:
\nproject/\n├── templates/\n│ └── hello.html.twig\n├── vendor/\n└── index.php\n\nTemplate: templates/hello.html.twig
<!DOCTYPE html>\n<html>\n<head>\n <title>Greeting</title>\n</head>\n<body>\n <h1>Hello, {{ name }}!</h1>\n <p>Today is {{ "now"|date("d.m.Y") }}.</p>\n</body>\n</html>\n\nPHP script: index.php
<?php\nrequire_once 'vendor/autoload.php';\n\nuse Twig\\Environment;\nuse Twig\\Loader\\FilesystemLoader;\n\n// 1. Create a loader that points to the templates folder\n$loader = new FilesystemLoader('templates');\n\n// 2. Create the Twig environment\n$twig = new Environment($loader, [\n 'cache' => 'cache', // Path for caching (recommended)\n 'auto_reload' => true, // Automatically reload on changes (for development)\n]);\n\n// 3. Render the template, passing variables\necho $twig->render('hello.html.twig', ['name' => 'World']);\n?>\nOpen index.php in your browser. You will see a page with the title "Hello, World!" and the current date.
Main Twig Methods and Functions
\nBelow are the key methods and constructs that form the basis of working with Twig.
\n\n1. Environment::render() — Template Rendering
\nSignature: public function render(string $name, array $context = []): string
Description: The main method for displaying a template. It takes the template file name (relative to the loader root) and an associative array of variables (context) that will be available inside the template.
\n<?php\n//