21.10.2015 Views

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 />

es/AcmeBlogBundle directory won't exist, so you'll need to create it). You're now free to<br />

customize the template.<br />

This logic also applies to base bundle templates. Suppose also that each template<br />

inAcmeBlogBundle inherits from a base template<br />

called AcmeBlogBundle::layout.html.twig. Just as before, Symfony2 will look in the<br />

following two places for the template:<br />

1. app/Resources/AcmeBlogBundle/views/layout.html.twig<br />

2. src/Acme/BlogBundle/Resources/views/layout.html.twig<br />

Once again, to override the template, just copy it from the bundle<br />

toapp/Resources/AcmeBlogBundle/views/layout.html.twig. You're now free to<br />

customize this copy as you see fit.<br />

If you take a step back, you'll see that Symfony2 always starts by looking in<br />

theapp/Resources/{BUNDLE_NAME}/views/ directory for a template. If the template<br />

doesn't exist there, it continues by checking inside the Resources/views directory of the<br />

bundle itself. This means that all bundle templates can be overridden by placing them in the<br />

correct app/Resources subdirectory.<br />

Overriding Core Templates<br />

Since the Symfony2 framework itself is just a bundle, core templates can be overridden in the<br />

same way. For example, the core TwigBundle contains a number of different "exception" and<br />

"error" templates that can be overridden by copying each from<br />

the Resources/views/Exception directory of the TwigBundle to, you guessed it,<br />

the app/Resources/TwigBundle/views/Exception directory.<br />

Three-level Inheritance<br />

One common way to use inheritance is to use a three-level approach. This method works<br />

perfectly with the three different types of templates we've just covered:<br />

Create a app/Resources/views/base.html.twig file that contains the main layout<br />

for your application (like in the previous example). Internally, this template is<br />

called::base.html.twig;<br />

Create a template for each "section" of your site. For example, an AcmeBlogBundle,<br />

would have a template called AcmeBlogBundle::layout.html.twig that contains<br />

only blog section-specific elements;<br />

{# src/Acme/BlogBundle/Resources/views/layout.html.twig #}<br />

{% extends '::base.html.twig' %}<br />

{% block body %}<br />

Blog Application<br />

{% block content %}{% endblock %}<br />

{% endblock %}<br />

Create individual templates for each page and make each extend the appropriate section<br />

template. For example, the "index" page would be called something close<br />

toAcmeBlogBundle:Blog:index.html.twig and list the actual blog posts.<br />

79

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!