How to Configure and Use the templating
Service
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:
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
1 2 3 4
# app/config/config.yml framework: # ... templating: { engines: ['twig'] }
- XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<!-- app/config/config.xml --> <?xml version="1.0" encoding="UTF-8" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:framework="http://symfony.com/schema/dic/symfony" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> <!-- ... --> <framework:config> <framework:templating> <framework:engine>twig</framework:engine> </framework:templating> </framework:config> </container>
- PHP
1 2 3 4 5 6 7 8
// app/config/config.php $container->loadFromExtension('framework', array( // ... 'templating' => array( 'engines' => array('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.