Technologies
Twig PHP
Twig is one of the most popular template engines written in PHP. It was created as a port of the Jinja engine and is used as the main engine template, including in the Symfony framework.
It allows you to separate the presentation layer from the rest of the application. It is equipped with many convenient functionalities and allows for easy expansion, making Twig a very flexible tool in the programmer's work. It is efficient, secure, and highly scalable, making it suitable for any project.
What is a template engine?
Template engines, also known as template processors, are libraries and software designed to combine templates with data models to produce documents (template files). They are most often used for developing web applications that are divided into separate components. They also allow you to render the server-side data quickly.
Characteristics of Twig
In the words of the Symfony framework creators (SensioLabs), Twig is characterized by three main features.
Speed
Twig compiles templates and turns them into optimized PHP code.
Safety
This template language has a sandbox that makes it possible to evaluate untrusted template code. Additionally, this makes it possible to use Twig for applications where users can modify the template design.
Flexibility
Twig possesses a flexible lexer and parser, meaning that it provides a developer with the ability to define their own custom tags and filters as well as create their own DSL.
Twig template engine and PHP
As we wrote above, Twig is a template engine written in PHP. PHP is a programming language that started as a template engine but evolved past that. Moreover, it doesn't support the features of modern template engines.
The main features that set apart PHP and Twig are as follows.
Being concise
PHP language is verbose, and this is particularly visible during the output of HTML content.
Template-oriented syntax
Twig templates make use of shortcuts for common patterns. For example, Twig displays a default text when iterating over an empty array.
Feature support
This template engine, among others, supports multiple inheritance, blocks, and automatic output-escaping. Thanks to this, you can easily build powerful templates.
Easiness of learning
The Twig syntax is easy to learn, which enables developers and web designers to work fast.
Extensibility
Twig can meet various needs thanks to, among others, an open architecture that allows you to implement language constructs such as tags, filters, functions, or operators. You can write and implement a custom Twig extension should you need.
Unit tested
Twig's library is fully stable, unit-tested, and ready to use in large projects.
Documentation
This template engine has full API documentation and a dedicated online book.
Security
Security features available include the following: automatic output escaping that can be enabled globally or for an individual block of code and sandboxing, thanks to which Twig can evaluate a template in a sandbox environment. It can also be enabled globally or only for some templates.
Clean Error Message
Thanks to clean error messages that are displayed when there's a problem with the syntax in the Twig code, you obtain information about a filename and the line number where the error occurred. This significantly speeds up and simplifies debugging work.
Fast
As mentioned, this template engine was built for speed, and to achieve that, Twig compiles a template file down to plain optimized PHP code, and the overhead in comparison to PHP code is reduced to a minimum.
How to install Twig?
There are a few ways in which you can install Twig. One of which is the installation of the Twig PHP package by installing Composer. You can also install a C extension that is compatible with PHP5. There is also the option of using the tarball that can be found on the SensioLabs website.
Why use Twig?
To summarize, the Twig template engine is safe, fast, flexible, and scalable. It enables the separation of logic and content. It's easy to learn, is fully unit-tested, and has complete documentation.
Additionally, as a small note for developers, the Twig API allows you to create your own loaders to load templates.