The Templating Component
Edit this pageWarning: You are browsing the documentation for Symfony 2.0, which is no longer maintained.
Read the updated version of this page for Symfony 6.2 (the current stable version).
The Templating Component
Templating provides all the tools needed to build any kind of template system.
It provides an infrastructure to load template files and optionally monitor them for changes. It also provides a concrete template engine implementation using PHP with additional tools for escaping and separating templates into blocks and layouts.
Installation
You can install the component in many different ways:
- Use the official Git repository (https://github.com/symfony/Templating);
- Install it via Composer (
symfony/templating
on Packagist).
Usage
The PhpEngine class is the entry point of the component. It needs a template name parser (TemplateNameParserInterface) to convert a template name to a template reference and template loader (LoaderInterface) to find the template associated to a reference:
1 2 3 4 5 6 7 8 9
use Symfony\Component\Templating\PhpEngine;
use Symfony\Component\Templating\TemplateNameParser;
use Symfony\Component\Templating\Loader\FilesystemLoader;
$loader = new FilesystemLoader(__DIR__ . '/views/%name%');
$view = new PhpEngine(new TemplateNameParser(), $loader);
echo $view->render('hello.php', array('firstname' => 'Fabien'));
The render() method executes the file `views/hello.php` and returns the output text.
1 2
<!-- views/hello.php -->
Hello, <?php echo $firstname ?>!
Template Inheritance with Slots
The template inheritance is designed to share layouts with many templates.
1 2 3 4 5 6 7 8 9
<!-- views/layout.php -->
<html>
<head>
<title><?php $view['slots']->output('title', 'Default title') ?></title>
</head>
<body>
<?php $view['slots']->output('_content') ?>
</body>
</html>
The extend() method is called in the sub-template to set its parent template.
1 2 3 4 5 6 7 8 9 10 11
<!-- views/page.php -->
<?php $view->extend('layout.php') ?>
<?php $view['slots']->set('title', $page->title) ?>
<h1>
<?php echo $page->title ?>
</h1>
<p>
<?php echo $page->body ?>
</p>
To use template inheritance, the SlotsHelper helper must be registered:
1 2 3 4 5 6 7 8
use Symfony\Component\Templating\Helper\SlotsHelper;
$view->set(new SlotsHelper());
// Retrieve page object
$page = ...;
echo $view->render('page.php', array('page' => $page));
Note
Multiple levels of inheritance is possible: a layout can extend an other layout.
Output Escaping
This documentation is still being written.
The Asset Helper
This documentation is still being written.