Twig is a template engine for PHP. This page is the entry point to this site's Twig articles, arranged in the order you can study them. Each article is checked against the official Twig 3.x documentation (where an article has an update note, the date of the check is at its top). The five articles on whitespace control, macros, attribute, concatenation, and length were also run on Twig 3.30.0 (PHP 8.4), and their results are shown.
Some syntax behaves differently in Twig 2.x or 1.x. If you do not know the Twig version of an older project, check it first, for example with `composer show twig/twig`.
STUDY ORDER
Basics 1: Variables, output, strings
Basics 2: Conditions and operators
Basics 3: Loops, arrays, counting
Advanced 1: Reusable parts, inheritance, macros
Advanced 2: Formatting output (whitespace control)
WHEN USING TWIG WITH SYMFONY
The next two articles are about Symfony features, not Twig itself. They were written in 2021 and include older Symfony conventions (annotations, `app/config/config.yml`, and so on). I have not yet checked how they differ from current Symfony. Read them as background and compare with the current official documentation before using them.
VERSION DIFFERENCES (CONFIRMED)
- ▸The `spaceless` filter is deprecated in Twig 3.12 (use the `-` and `~` whitespace control instead). The whitespace article shows the output of running it.
- ▸The official documentation marks the `attribute()` function as deprecated since Twig 3.15. The attribute article shows the current way to write it.
- ▸A macro defined in the same template can be called through `_self` without importing it. This was confirmed in the macro article.
HOW THIS GUIDE IS CHECKED
The code I actually ran on Twig 3.30.0 / PHP 8.4 in Docker and checked is the code in the five articles on whitespace control, macros, attribute, concatenation, and length. The other articles were checked against the official documentation and were not run when this guide was made; the articles themselves mark what is "stated in the official documentation". The official documentation is at https://twig.symfony.com/doc/3.x/ .
