Skip over navigation

Contact us to learn more about OroCommerce's capabilities

Contact us

Developers' Digest

Using PHP instead of Twig for templates

August 31, 2016 | Oro Team

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:

        default: php
            enabled: false

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:

        - @setBlockTheme:
            themes: 'MyBundle:layouts/first_theme/php'
        - @addTree:
                    blockType: head
                    blockType: meta
                        http_equiv: Content-Type
                        content: "text/html; charset=utf-8"
                    blockType: body
                    blockType: container
                            class: content
                    blockType: block
                        meta: ~
                            greeting: ~

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):

        - @setBlockTheme:
            themes: 'MyBundle:layouts/first_theme/php'

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!”:



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:

<div <?php echo $view['layout']->block($block, 'block_attributes') ?>>
    <h1>Welcome back</h1>
    <?php echo $view['layout']->widget($block); ?>

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

<!DOCTYPE html>
    <head class="foo">
        <meta http_equiv="Content-Type" content="text/html; charset=utf-8"/>
        <div class="content">
            <h1>Welcome back</h1>

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.

Back to top