Getting started
If you are already familiar with writing Twig templates, you might want to skip this part and directly head over to the next chapter. For everyone else, here comes a quick Twig 101.
Why Twig in Contao?
Twig is the standard way to write templates in Symfony. And that is for a reason: It features a wide range of powerful methods to structure and reuse templates, has an easy-to-use syntax to access objects, helpers to transform data, built-in whitespace control, string interpolation features, macros, and β really β a ton moreβ¦
But ultimately, the biggest selling point for us is security. Twig embraces output encoding, something that we desperately try to move to for a long time. That is, why Twig has been made a core requirement and will replace our old existing template system and also why you should familiarize yourself with it.
Learning the syntax
Twig templates have their own syntax, but don’t be afraid, you’ll quickly find your way. Here, is an example of a PHP template, that was translated into an equivalent Twig template. (In case you’re new to Contao, you might want to directly focus on the Twig side and ignore the old stuff. π)
- To output parameters, we wrap their name in curly braces
{{ foo }}, - to use keywords β like
forto loop over an array β we wrap them in{%and%}, - to further process any output, we use filters
|fooand functionsbar(), - to concat variables and strings, we use the
~operator (see here for more operators). - Finally, to add comments, we put them between
{#and#}.
Twig is very well documented. A good place to start is the Twig for template designers section that covers syntax details as well as helpful IDE plugins for autocompletion and syntax highlighting.
Tip
For quickly trying something out, you can use the Twig Playground. Take a look at this demo for instance.
Extending Twig
Extending Twig yourself is easy but there are also already a lot of Twig extensions in the wild, including some official ones, called the twig-extra bundles. In Contao, the latter can simply be installed with Composer or the Contao Manager and are directly ready to be used (no need to configure or register anything).
Custom code
We also encourage you to extend Twig yourself. There are a lot of extension points with filters and functions being the easiest to start with. Have a look in the official docs, where things are explained in more detail.
Info
If you created PHP templates in the past, and are missing functionality that you previously would have implemented directly in the templates, using filters and functions is the way to go. As a nice side effect, you can now make use of your custom implementation in any template.
Creating your first Twig filter
Let’s implement a simple |rot13 filter in our application, that will scramble strings by shifting every letter by 13
places in the alphabet (Abcd β Nopq). That’s apparently so useful, that PHP already has a built-in function for
this: str_rot13(). Nice.
Go ahead and create a class, that is extending Twig\Extension\AbstractExtension in the src/Twig folder of your
application. You can have as many extensions as you want but in an application, you would typically only use a single
AppExtension. Then, override the getFilters() function and register a new filter:
Andβ¦ that’s all there is to it. Our filter is now ready and can be used in any template: