How to Configure and Use the templating Service

Version: 2.7
Edit 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 5.3 (the current stable version).

How to Configure and Use the templating Service

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:

  • YAML
  • XML
  • PHP
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.