Recently there has been some buzz around Magento getting support for Twig template engine. Looking at the CHANGELOG.markdown file under the Magento2 GitHub repository for 220.127.116.11-dev44 version commit, this seems to be more than just a buzz. Here are the specific log entries mentioning the Twig:
- Introduced support for Twig templating
- template rendering, including phtml, was abstracted into a Mage_Core_Block_Template_Engine to make support for other template engines easier
- included Magento-specific Twig functions and filters
- Converted product view page to demonstrate use of Twig templates and services
So what exactly is Twig? Twig is a modern template engine for PHP, you can get it from http://twig.sensiolabs.org. It is fast, secure and flexible, at least the “commercial” says so :). Twig is developed by Fabien Potencier, the creator of the Symfony framework, and it is released under the new BSD license.
Even today, in 2013, there are many developers that will argue how PHP itself is a template engine itself so adding something like specialised template engines on top of it is an overkill. When you look at the Magento 1.x series, it uses pure PHP mixed with HTML within its *.phtml files, and the whole thing works. Well, it works more or less, until you get your developer assembling collection objects and coding in hundred of lines of business logic within .phtml file, logic that needs to be shared across few controllers, etc. Then the whole thing gets bunch of duplicated code, etc.
There are strong arguments why use template engine with PHP. I would strongly recommend reading the Templating Engines in PHP article written by Fabien Potencier himself. Be worn doe, author himself has an article intro starting with “This blog post is not for the faint-hearted!”. I personally agree with his views and comments on the modern template language feature requirements such as: concision, template oriented syntax, reusability, security, etc. With that said, I personally welcome the support for Twig within Magento2.
So where are we with Magento 2 + Twig implementation. If you git clone https://github.com/magento/magento2.git project and pull it into your favorite IDE (NetBeans) then do a Find for “twig” string, you will get a result of “Found 4,944 matches of twig in 397 files.”. There is no point in listing all those files here, this is just to get a glimpse on things. Since everything major and core in Magento start from Mage_Core, if you do a same search on the /app/code/Mage/Core/ folder you will get a result of “Found 47 matches of twig in 4 files.”. Now this is the number of files we can start tracing from.
You can clearly see that there is a Mage_Core_Block_Template_Engine_Twig class that implements the Mage_Core_Block_Template_EngineInterface interface. I must say, this is a nice surprise, seeing Magento using interfaces. You can take this as a bit of sarcasm from my side given that Magento 1.x was all about class extending. Surprisingly, Mage_Core_Block_Template_EngineInterface interface is ultimately simple, it only holds one method “public function render(Mage_Core_Block_Template $block, $templateFile, $vars);”. Looking further at the Mage_Core_Block_Template_Engine_Factory class and its “public function get($name)” method, seems like built in stuff will cover standard *.phtml and new Twig templates.
I won’t go into further details about implementation and usage, maybe in some of my later articles, when I actually get to play with it.
To conclude, what I am personally hoping that the Twig templating support will achieve for Magento 2 is slightly cleaner 3rd party extension code in a first place. Primarily hoping not to see any more collection and business logic code within yesterdays *.phtml files, etc.
Would be interesting to hear your thought about this Magento move?