TWIG is an open source theme engine based on the PHP language. TWIG is widely used by front end developers in order to create captivating themes and user interfaces. Although PHP itself can be used as template engine, it's verbose and not easy to learn. Therefore TWIG has been developed with focus on front end development and theming. TWIG is also easier to learn.
Looking at the power and usability of TWIG, Drupal’s developer community has decided to replace its older PHP template engine in the upcoming Drupal 8 release. The decision has been taken because of several issues in the Drupal 7. TWIG solved most of the bottlenecks and overheads occurred by using the older templates. Let us explain what are some of the issues in the Drupal 7 that TWIG aims to resolve in the next version of Drupal, the Drupal 8.
Problems in Drupal 7, Likely to be resolved by TWIG in Drupal 8
Multiple Data Printing Ways
In older Drupal 7, there were two ways to print the variable. User could either use the print keyword to print some variable and render keyword to display other objects. It was difficult for user to decide what whether to print the variable via print or render.
Solution: In TWIG, this problem has been resolved and user just needs to use a pair of curly brackets before and after the variable like {{ variable }} and TWIG takes care of whether to print it or render it.
Mixed Data Types
In Drupal 7, data types in the templates could be arrays, strings or objects and user had no clue about the type of the variable being used.
Solution: In TWIG, “dot” operator can be used to dig into the variable and the correct data type inside the variable can be accessed using the dot operator.
Multiple Ways to Override the Markup
Currently in Drupal 7, there are two ways to override the markup that comes with Drupal 7. Either copy the template in file system and change the markup or use the PHP Theme functions in order to override the markup.
Solution: In TWIG, all of the PHP theme functions will be replaced by TWIG templates therefore there will be no need to us verbose and complex PHP theme functions.
No Reusability
In Drupal 7, there are multiple theme functions and templates performing nearly the same function due to lack of any design pattern and standard rules.
Solution: TWIG introduces patterns and standard set of rules which govern front end developers to use the already available theme and function rather than developing everything from the scratch.
Security
Drupal is not secure, using node object, a user can access any variably by digging deep into several levels of a variable which can be an object or data structure. This access to complex algorithms makes Drupal 7 vulnerable.
Solution: TWIG hides this complex and internal information from the user. In Drupal 8, users will just have to give something like a print command and TWIG will take care of all the internals issues. User will have no direct access to executable PHP template functions.
Language and Syntax Independence
The theme engine used by Drupal 7 is Drupal Specific. All the terms and languages are not used outside Drupal. This makes it difficult for new developers to find their feet in Drupal community.
Solution: Twig is syntactically more similar to existing web languages on web such as HTML. A new user will find it easier to get use to the Drupal 8 development syntax.
Other Cool Twig Features in Drupal 8
Apart from the issues in Drupal 7 that TWIG is anticipated to resolve in Drupal 8, it has several other cool features. These features are as follows
TWIG templates can be edited inside a web browser.
TWIG can be used with both frond end and back end technologies like PHP and JavaScript enabled Ajax on the front end.
Full duplex communication capability between the code and front end which means that user will be able to make changes in front end from code and vice versa.
TWIG is extremely well documented and Drupal has faced documentation issues in the past therefore TWIG is an excellent choice as a template engine in Drupal 8.
TWIG is easily extensible.
TWIG has excellent integration capabilities and it integrates well with a variety of IDEs.
TWIG has been developed by Symphony which has also developed multiple extensions for Drupal. This similarity of development team also endorses TWIG as a template engine.
Last but certainly not the least, TWIG is the fasted theme engine and it is certainly faster than the theme engines previously used by Drupal such as smarty.
Overall, using TWIG as theme engine in Drupal 8 is an excellent decision made by the Drupal developer’s community. It is robust, secure, fast and usable. It is anticipated that Drupal 8 will be a market hit and is like to give tough time to other similar content management systems.
References
Some links with more information that served as inspiration for this post:
http://rootwork.org/blog/2013/05/drupal-8-power-twig-drupalcon-portland-featured-session
https://portland2013.drupal.org/session/using-twig-new-template-engine-drupal-8
http://www.appneta.com/blog/twig-performance/
http://drupalmotion.com/article/drupal-twig-templates
http://pingv.com/blog/drupal-8-coming
http://www.jenlampton.com/presentations/new-theme-layer-drupal-8-2
The Twig logo is © 2010-2012 Sensio Labs