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:
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:
oro_layout: templating: default: php twig: 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:
#MyBundle/Resources/views/layouts/first_theme/default.yml layout: actions: - @setBlockTheme: themes: 'MyBundle:layouts/first_theme/php' - @addTree: items: head: blockType: head meta: blockType: meta options: http_equiv: Content-Type content: "text/html; charset=utf-8" body: blockType: body content: blockType: container options: attr: class: content greeting: blockType: block tree: root: head: meta: ~ body: content: 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):
actions: - @setBlockTheme: themes: 'MyBundle:layouts/first_theme/php'
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:
#MyBundle/Resources/views/layouts/first_theme/php/_content_widget.html.php <div <?php echo $view['layout']->block($block, 'block_attributes') ?>> <h1>Welcome back</h1> <?php echo $view['layout']->widget($block); ?> </div>
The layout and templates from our examples will produce the following HTML output:
<!DOCTYPE html> <html> <head class="foo"> <meta http_equiv="Content-Type" content="text/html; charset=utf-8"/> </head> <body> <div class="content"> <h1>Welcome back</h1> <p>Hello!</p> </div> </body> </html>
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.