Twig
By SensioLabs / Symfony
Twig is a template engine for PHP, created by SensioLabs and used as the default templating layer in the Symfony framework, that provides a concise, sandbox-friendly syntax for expressions, control structures, filters, and template…
Definition
Twig is a template engine for PHP, created by SensioLabs and used as the default templating layer in the Symfony framework, that provides a concise, sandbox-friendly syntax for expressions, control structures, filters, and template inheritance while remaining separate from raw PHP code inside view files. It compiles templates into optimized, cached PHP classes so rendering stays fast despite the added abstraction over hand-written PHP.
Overview
Twig was created to give PHP developers a templating layer distinct from writing raw PHP inside view files, addressing concerns that mixing full PHP syntax directly into templates makes them harder to secure, read, and hand off to designers who are not PHP developers. Its syntax uses `{{ }}` for output, `{% %}` for statements such as loops, conditionals, and blocks, and `{# #}` for comments, borrowing conventions later echoed by other engines such as Nunjucks and Twig-inspired ports in different languages. Underneath, Twig compiles each template into a PHP class with a render method, and that compiled class is cached on disk so subsequent requests skip re-parsing the original template source, which keeps rendering performance close to hand-written PHP despite the extra abstraction layer. Twig also supports a sandbox mode that restricts which tags, filters, and functions a template can access, a feature aimed at applications that let untrusted users edit templates, such as a CMS with user-customizable themes. As the default templating engine bundled with Symfony, Twig sits alongside Blade in Laravel as one of the two dominant PHP template engines, differing from Blade primarily in its non-PHP-like syntax and independent sandboxing model, and it differs from older approaches like Smarty by favoring inheritance and blocks over Smarty's earlier custom tag philosophy. Twig's inheritance model, using `{% extends %}` and `{% block %}`, closely parallels the pattern later adopted by Jinja-family and Nunjucks templates, and in fact influenced or paralleled Jinja's own design given shared contributors between the PHP and Python templating communities. In practice, Twig is used throughout Symfony applications to render HTML views, and it has also been adopted independently by other PHP projects and content management systems that want its inheritance and filter system without adopting the full Symfony framework. Its extensive filter and function library covers common needs like date formatting, string manipulation, and array operations directly within template expressions. Twig's separation from raw PHP is also a limitation for developers who want to drop into arbitrary PHP logic inside a view, since Twig deliberately does not expose PHP's full function library or control structures unless explicitly registered as a custom filter or function by the application. This design trade-off favors safety and consistency over flexibility, and teams that want templates closer to plain PHP syntax typically choose Blade or plain PHP templates instead. Twig's authors maintain the balance deliberately, arguing that a constrained template language keeps large teams from accumulating logic in views that belongs in application code instead.
Key Features
- Non-PHP syntax that separates template markup from raw PHP code
- Template inheritance through extends and overridable named blocks
- Sandbox mode restricting tags and functions for untrusted templates
- Compiles templates into cached PHP classes for fast rendering
- Extensive built-in filter and function library for common formatting
- Default templating engine bundled with the Symfony framework
- Supports custom filters, functions, and tags via extensions