1-33
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Symfony2 – Franz Jordán 2011<br />
Configuring and using the templating Service<br />
The heart of the template system in Symfony2 is the templating Engine. This special object is<br />
responsible for rendering templates and returning their content. When you render a template in<br />
a controller, for example, you're actually using the templating engine service. For example:<br />
return $this->render('AcmeArticleBundle:Article:index.html.twig');<br />
is equivalent to<br />
$engine = $this->container->get('templating');<br />
$content = $engine->render('AcmeArticleBundle:Article:index.html.twig');<br />
return $response = new Response($content);<br />
The templating engine (or "service") is preconfigured to work automatically inside Symfony2. It<br />
can, of course, be configured further in the application configuration file:<br />
# app/config/config.yml<br />
framework:<br />
# ...<br />
templating: { engines: ['twig'] }<br />
Several configuration options are available and are covered in the Configuration Appendix.<br />
The twig engine is mandatory to use the webprofiler (as well as many third-party bundles).<br />
Overriding Bundle Templates<br />
The Symfony2 community prides itself on creating and maintaining high quality bundles<br />
(seeSymfony2Bundles.org) for a large number of different features. Once you use a third-party<br />
bundle, you'll likely need to override and customize one or more of its templates.<br />
Suppose you've included the imaginary open-source AcmeBlogBundle in your project (e.g. in<br />
thesrc/Acme/BlogBundle directory). And while you're really happy with everything, you want<br />
to override the blog "list" page to customize the markup specifically for your application. By<br />
digging into theBlog controller of the AcmeBlogBundle, you find the following:<br />
public function indexAction()<br />
{<br />
$blogs = // some logic to retrieve the blogs<br />
$this->render('AcmeBlogBundle:Blog:index.html.twig', array('blogs' =><br />
$blogs));<br />
}<br />
When the AcmeBlogBundle:Blog:index.html.twig is rendered, Symfony2 actually looks<br />
in two different locations for the template:<br />
1. app/Resources/AcmeBlogBundle/views/Blog/index.html.twig<br />
2. src/Acme/BlogBundle/Resources/views/Blog/index.html.twig<br />
To override the bundle template, just copy the index.html.twig template from the bundle<br />
toapp/Resources/AcmeBlogBundle/views/Blog/index.html.twig (theapp/Resourc<br />
78