English Thai

Twig an Advanced Template Engine for PHP

Twig is a modern, optimized template engine for the PHP programming language developed by Fabien Potencier. It is an open source product licensed under a BSD License, initially created by Armin Ronacher. Due to its uncomplicated syntax and straightforward processes, Twig is a widely praised template engine in the PHP community.

Fabien Pontencier discovered Twig as he was looking for a PHP template language. He continued Ronachers work, who wrote it back in ’08 for a blogging platform but stopped developing it to pursue working with Python. While developing Twig, Pontencier wrote a documentation about it and created a website for it.

But why is Twig so Highly Praised?

Twig is fast, it complies templates down to plain PHP. Thereby Twig reduces the amount of PHP overhead to a very minimum, producing a faster, smoother user experience.

Twig is very concise compared to the PHP language leading to a more readable templates.
Supported by its flexible lexer and pars
er, Twig is very extensive. Making it possible for the developer to devise his own custom tags and filters. Further it allows to create its own DSL. All thanks to an open architecture.

Twig doesn’t conserve in view of security, giving the user some unique features. You can now activate automatic output escaping globally or for a block code, just to be sure. Furthermore, Twig has a sandbox mode which enables you to verify any untrusted code. Resulting in the possibility to use Twig as a template language for applications and still using it to modify the template design.
In addition, Twig offers abbreviations for common patterns, like displaying a default text when you go over an empty array.
Twig is also full featured. Which means it supports all you need to build templates with ease. For example: blocks, automatic output-escaping, multiple inheritance and more. But although being so full featured, Twig is still the fastest PHP template engine.

Library Time(sec) Memory(Ko) Templates rendered per second
Twig 3 1,190 3,333
PHPTAL 3.8 2,100 2,632
Dwoo 6.9 1,870 1,449
Smarty 2 12.9 2,350 775
Smarty 3 14.9 3,230 671
Calypso 34.3 620 292
eZ Templates 53 5,850 189

Plus Twig consumes less memory:

Library Memory without compilation (Ko)
Twig 383
Dwoo 1,645
Smarty 2 1,634
Smarty 3 1,790
Calypso 614
eZ Templates 2,783