Technologies

TWIG

Twig is the default templating tool for Symfony. It's a powerful and fast tool for creating structured HTML display code templates that are easy to work with.

What is a templating engine and how does Twig work?

Twig is a part of a View in an MVC pattern. Model-View-Controller is a pattern that, like the others, divides the application into three parts:

01
01
Model

The database and the main application logic for working with it, processing queries, performing calculations, etc. It is independent of the other two parts. When the Controller tells it how to change, the Model creates a corresponding event. The View subscribed to it requests the updated data and displays it.

02
02
View

View - a user interface, all visual elements with which the user will interact. All events in the View affect only the Controller.

03
03
Controller

Responds to external events in the View (if the user clicks a button or enters new data) and tells the model how to change.

The templating engine, and Twig in particular, is responsible for presenting the data that will be shown to the user. This is a text file with html code and tags, CSS styles, Javascript code, and other page content. The main difference from a regular page is the presence of template structures. These are various service functions that are responsible for automating the change of displayed data and simplifying its presentation. Twig, as a templating engine, simply structures information from the web server before it is passed to the Controller and then to the user.

Three reasons to use Twig for your project
Simple target syntax

One of the peculiarities of PHP is that it has very complicated and long instructions for output escaping. Twig solves this problem by making templates concise and clear. Its syntax has a lot of shortcuts for setting default parameters. Together, this speeds up development and reduces the likelihood of errors.

Speed and flexibility

Compiling templates into PHP code with Twig is much faster and easier, which significantly saves development costs. At the same time, the templating engine can be adapted to the tasks of any project. Your developers will be able to add their own tags, filters, operators, and so on. This way, you will have a unique DSL that is fully focused on the goals of a project of any scale and at the same time is consistently simple and stable.

All the necessary functionality

Even complex templates are easy to create with Twig, thanks to support for all the tools you need, such as multiple inheritance. Many of these features are aimed at ensuring security, such as automatic output escaping and a sandbox for testing.

Conclusion

Twig is a library for writing templates that optimize HTML code for display. It is not the only PHP templating engine, but it is recommended by Symfony, the most powerful framework written in this language. Twig is the simplest and most flexible solution for creating templates of any complexity. If you connect this library, you can speed up development and testing several times, and get a more productive and stable system as a result.