Slots HelperEdit this page
Warning: You are browsing the documentation for Symfony 2.7, which is no longer maintained.
Read the updated version of this page for Symfony 6.0 (the current stable version).
More often than not, templates in a project share common elements, like the well-known header and footer. Using this helper, the static HTML code can be placed in a layout file along with "slots", which represent the dynamic parts that will change on a page-by-page basis. These slots are then filled in by different children template. In other words, the layout file decorates the child template.
The slots are accessible by using the slots helper (
display the content of the slot on that place:
1 2 3 4 5 6 7 8 9 10 11 12
<!-- views/layout.php --> <html> <head> <title> $view['slots']->output('title', 'Default title') </title> </head> <body> $view['slots']->output('_content') </body> </html>
The first argument of the method is the name of the slot. The method has an optional second argument, which is the default value to use if the slot is not available.
_content slot is a special slot set by the
PhpEngine. It contains
the content of the subtemplate.
If you're using the standalone component, make sure you registered the SlotsHelper:
1 2 3 4
use Symfony\Component\Templating\Helper\SlotsHelper; // ... $templateEngine->set(new SlotsHelper());
The extend() method is called in the
sub-template to set its parent template. Then
can be used to set the content of a slot. All content which is not explicitly
set in a slot is in the
1 2 3 4 5 6 7 8 9 10 11
<!-- views/page.php --> $view->extend('layout.php') $view['slots']->set('title', $page->title) <h1> echo $page->title </h1> <p> echo $page->body </p>
Multiple levels of inheritance is possible: a layout can extend another layout.
For large slots, there is also an extended syntax:
1 2 3
$view['slots']->start('title') Some large amount of HTML $view['slots']->stop()