Stefano CappelliniSC

AI • Deep Learning • Machine Learning • Software Engineering

Why learn Symfony? Part 1 - Twig

Written on

; In: ,

Why learn Symfony? Part 1 - Twig

Note: this is an article I wrote for handsonsymfony.com. You can also read it at the following link: handsonsymfony.com/en/blog/why-learn-symfony-pt-1-twig/

Let's begin this new blog series, "Why learn Symfony?", with the first reason: Twig, Symfony's template engine.

There are many PHP template engines out there, but Twig is surely one of the most mature and complete. Although you can easily use it with almost any other framework, it is nicely integrated with Symfony (it's the default template engine) and comes with lots of add-ons. That's why we've decided to list it here. Here are 8 good reasons to use it.

1: Easy syntax, easy to learn and perfect for template designers

With its clear, concise Python-like syntax — heavily inspired by Jinja and Django's templates — Twig is easy to learn and use even for non-developers and for template designers. It doesn't require, for example, the dollar sign

Look mum, no dollar sign, {{ my_obj }}!

and it has a very nice and short filter syntax to easily process your data

{{ my_text|upper }}
{{ "    Hello World     "|trim|capitalize }}

2: Separation of concerns

You cannot use plain PHP code inside your template files, and that's truly a great thing. This helps keeping the model and the controllers well separated from the views, increasing your codebase's overall quality. In addition, this also means that your frontend team won't have to learn PHP to be able to work on your templates.

3: Security

In Twig, every expression is automatically escaped, reducing the likelihood of a XSS attack. In addition, it comes with various different filtering functions (to escape HTML content, JS content, attributes content and so on).

However, the real Twig's killing feature is its sandbox. Sandbox allows you to safely evaluate untrusted template snippets, specifying which functions are allowed inside them and which instead are to be discarded. With a sandbox as that you could, for example, allow users to customize their own pages' template.

4: Powerful and extensible

With Twig, you get lots of features (filters, functions, …) out of the box. In addition, it is also extremely easy to extend, adding the functionalities your frontend team actually needs in order to correctly present your data. All the extensions are written in plain PHP (thus there's no need to learn a new language and you can also reuse part of your code) and are completely separated from your templates files: there is no need to insert some hacky code inside them.

5: Object Oriented

Twig is naturally Object Oriented. You get template inheritance, variable overriding, traits and composition almost for free.

6: Speed

All templates compile to PHP files. This means that, in production, the overhead for the template engine is really low.

7: Client side rendering

As stated before, you cannot put PHP code directly inside your templates. This means that Twig is not bound to PHP. You can, for example, use it with JavaScript and perform client side rendering, take a look at Twig.js! This is probably not something you will need often, but it's definitely a feature nice to have.

8: Unified powerful accessor

This is one of our favourite features, because it hides implementation details making easy to change your code in the feature without touching your templates (again, separation of concerns). In Twig, with this expression:

{{ obj.prop }}

you can access to both: array properties, object properties, getters (getProp), isser (isProp) and hasser (hasProp) methods.


This concludes our first post on "Why learn Symfony?". We already look forward to sharing with you our next post in this series. See you soon.