Prestashop 1.7, Symfony and Twig

Prestashop 1.7 will introduce the PHP framework Symfony, since the very first version. What will this mean, for developers?

Prestashop 1.7 code changes summary

  • PrestaShop 1.7 will introduce a brand new architecture, based on the Symfony Framework, along with its Twig templating engine.
  • The new architecture will only be used in the back office for now, and at first (1.7) only for two pages of the back office: the Product page and the Modules page.
  • The rest of the back office will still use the legacy architecture from 1.5-1.6 – but they will be switched to the new architecture in later versions of PrestaShop
  • The two architectures will coexist while we switch more back office pages to the new architecture, in a transition phase that will take a few versions of PrestaShop.
  • Only modules that target the Product and Modules pages will need to see their templating code adapted.

This is coming directly from the official PrestaShop Development Blog.

Whichs part of PrestaShop will be rewritten using Symfony?

The official blog itself states that PrestaShop 1.7 will not be 100% written in Symfony . Apparently, only part of the back office, namely the Product and the Modules pages, will be affected. The rest will be left untouched, meaning everything will still be based on the old Object Model / Hooks architecture.
They are obviously planning to expand the usage to cover more pages, little by little, with every new version.

As you probably know, PrestaShop is currently using Smarty as template engine for both the front and back office. This is not going to happen on pages using Symfony. Instead, they will use its own template engine, named Twig. I am not the biggest fan of Twig, but I guess we will have to deal with it.

The front office will not use Symfony, however, 1.6 themes will not, by any mean, be compatible with 1.7.

A good news for Modules Developer

The only modules that are going to be affected by this change are the ones hooking to the Product or Module back office. No fear though, we will not have to change anything of our modules since day 1. Instead, the PrestaShop team is setting up a switch that will let users run modules in legacy mode if they have not been rewritten in Twig.

Notice I am referring to Twig, and not Symfony, because the display part only would be affected. We will not be required to change anything of the PHP codebase, at least not at the beginning.

Symfony2 and Twig

Symfony PHP Framework

What are these, exactly? For the ones who have never heard it, Symfony is a PHP framework (like Zend, Laravel, or CodeIgniter). As such, it provides several helpers and components to make our life easier when coding anything in PHP.

The Team’s decision to choose version 2 if however arguable: Symfony 3.0 has been released last november, and this means PrestaShop’s “new” core architecture will be outdated before even having seen the light. I strongly hope they will drop Symfony2 in favor of version 3 as soon as they can.

Twig - Template Engine for Symfony

As for Twig, it’s a full-fledged template engine created by the same developers of Symfony. It’s very similar to Smarty, so, the transition shouldn’t be overly traumatic.

Differences between Twig and Smarty

Here are a few example of the main differences between the two engines.

Variable assignment and template display

Smarty in PrestaShop 1.6

	$this->context->smarty->assign(array('the' => 'variables', 'go' => 'here'));
	return $this->display(__FILE__, 'template.tpl');

Twig

	echo $twig->render('index.html', array('the' => 'variables', 'go' => 'here'));

Variables in templates

Smarty in PrestaShop 1.6

	{$variable.name}

Twig

	{{ foo.bar }}
	{{ foo['bar'] }} // both are fine, this is just an alternative

Control structures in templates

Smarty in PrestaShop 1.6

	
	{if ($shop_phone)}
		{l s='Tel: %s' sprintf=[$shop_phone|escape:'html':'UTF-8'] pdf='true'}
	{/if}
	{foreach from=$array item=myitem}
		<p>{$item}</p>
	{/foreach}

Twig

{% if users|length > 0 %}
	<ul>
	    {% for user in users %}
	        <li>{{ user.username|e }}</li>
	    {% endfor %}
	</ul>
{% endif %}

Bottom Line

We can’t tell if the transition to Symfony will bring fresh air in te PrestaShop development flow, at this stage. The team’s intentions seem serious enough though, so it’s strongly advisable to start learning Symfony and Twig as soon as you can.

You like the tuts and want to say "thank you"? Well, you can always feel free to donate:

  • mee

    ahh crap, it looks terrible lol… i like things simple, but twig looks like a whole new language to learn.

  • Yes

    Curses! I’ve just got used to Smarty:(

You like the tuts and want to say "thank you"? Well, you can always feel free to donate: