Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Components
  4. The Templating Component
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Installation
  • Usage
  • Template Inheritance with Slots
  • Output Escaping
  • The Asset Helper

The Templating Component

Edit this page

Warning: You are browsing the documentation for Symfony 2.1, 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 2 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.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Become certified from home

Become certified from home

Check Code Performance in Dev, Test, Staging & Production

Check Code Performance in Dev, Test, Staging & Production

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Billie Thompson, a Symfony contributor

Thanks Billie Thompson for being a Symfony contributor

1 commit • 276 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia