Skip to content

How to Configure and Use the templating Service

Warning: You are browsing the documentation for Symfony 2.x, which is no longer maintained.

Read the updated version of this page for Symfony 7.2 (the current stable version).

The heart of the template system in Symfony is the templating Engine. This special object is responsible for rendering templates and returning their content. When you render a template in a controller, for example, you're actually using the templating engine service. For example:

1
return $this->render('article/index.html.twig');

is equivalent to:

1
2
3
4
5
6
use Symfony\Component\HttpFoundation\Response;

$engine = $this->container->get('templating');
$content = $engine->render('article/index.html.twig');

return $response = new Response($content);

The templating engine (or "service") is preconfigured to work automatically inside Symfony. It can, of course, be configured further in the application configuration file:

1
2
3
4
# app/config/config.yml
framework:
    # ...
    templating: { engines: ['twig'] }

Several configuration options are available and are covered in the Configuration Appendix.

Note

The twig engine is mandatory to use the webprofiler (as well as many third-party bundles).

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version