ssossossosso
Blog

Covering OroCommerce topics, B2B eCommerce trends, community updates and company announcements.

Using PHP instead of Twig for templates

by add comment

Using PHP instead of Twig for templates

Symfony, the underlying framework used in OroCommerce, OroCRM and OroPlatform, defaults to Twig for its template engine, but using PHP instead of Twig is still possible if necessary. Symfony provides equally good support of both templating engines.

If you preference is PHP templates, this article describes how to enable and use PHP templates with OroLayout bundle in the applications built on OroPlatform.

The Symfony framework documentation contains additional useful information about PHP templates and form rendering customization:

Configure OroLayoutBundle

Only one templating engine can be used at a time in an OroPlatform application. By default, OroLayoutBundle is configured to use Twig. If you decide to use PHP templates, you should disable Twig and make PHP templating the default templating engine in the application configuration file:

Modify layouts to use PHP templates

The default “base” OroPlatform theme uses Twig templates. You should choose a different approach in your default.yml file in your theme’s folder:

The example above creates a standard web page structure (head, metadata, and body) with two custom blocks in the body (content and greeting). And in this layout, we specified a different “block theme” (so that the templating engine will know where to find our PHP templates):

Creating templates

As you are not using Twig anymore, you should provide the PHP templates for the blocks used in the layout.

The PHP templates can be very simple, like in the following example of the greeting block template where we just want to display “Hello!”:

Hello!

You can also create more complex templates that use variables and functions provided by the layout. This is an example of the content block template:

The layout and templates from our examples will produce the following HTML output:

A number of fully working PHP templates for various block types are already included in OroLayoutBundle – check the src/Oro/Bundle/LayoutBundle/Resources/views/Layout/php folder to see all the examples.

We prefer to use Twig in our products (e.g. see the default theme in OroCommerce) to better express presentation and to avoid including the program logic in the templates. However, using PHP instead of Twig may be better for you based on the needs of your customers and the approach you selected to build your OroPlatform-based application.

As always, please let us know if you find this article useful. We will appreciate your feedback in our forums.