Using PHP Instead of Twig for Templates¶
Symfony, the underlying framework used in OroCommerce, defaults to Twig for its template engine, but you can still use plain PHP templates if necessary. Symfony provides equally good support of both templating engines.
Note
It is recommended to use Twig in OroCommerce, like in the default theme in OroCommerce, to better express presentation and to avoid putting any application logics in the templates.
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.
Note: 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:
1 2 3 4 5 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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):
1 2 3 | actions:
- @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!”:
1 2 | #MyBundle/Resources/views/layouts/first_theme/php/_greeting_widget.html.php
<p>Hello!</p>
|
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:
1 2 3 4 5 | #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:
1 2 3 4 5 6 7 8 9 10 11 12 | <!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.