Magento 2 and Twig

Magento 2 and Twig

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 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 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 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?

You made it all the way down here so you must have enjoyed this post! You may also like:

Symfony templating with Twig Zoran Salamun
Zoran Salamun, | 0

Symfony templating with Twig

Programmatically add a custom block in Magento Admin Kristijan Glibo
Kristijan Glibo, | 4

Programmatically add a custom block in Magento Admin

Fancy Magento Global Messages Davor Budimcic
Davor Budimcic, | 29

Fancy Magento Global Messages


  1. I spoke to Ben Marks at PHPNW14 and he said it was too much work at that stage to get Twig into 2.0 rc, but was definately something they would look into for later releases.

  2. This will make life so much easier. Sandboxing template files alone will be worth all the effort to implement twig!

  3. I am not a fan of templating engines especially for #PHP.
    I used to hate #Smarty just because you have to invest time learning the syntax even you know PHP well.
    I have heard about the ‘Blade Template Engine’ used by #Laravel framework which seems to be quite promising.

    Not sure about the #Twig but let’s hope this integration makes both party happy.

  4. personally im not the biggest fan of additional template languages, but i admit that they bring some benefits

    what i would really want to see in magento, and hopefully integration of a twig will bring us closer to, is 100% separation of views from controllers/helpers

  5. I agree. Overall the move to a template language to abstract remove pure php logic from templates is a good move. Also it will open the door for many design centric frontend developers that are more comfortable with Expression Engine and the like. The more quality people the community can attract the better. A saying comes to mind, “Rising tide lifts all boats.”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.