1-33
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Symfony2 – Franz Jordán 2011<br />
This section will teach you the philosophy behind including stylesheet and Javascript assets in<br />
Symfony. Symfony also packages another library, called assetic, which follows this philosophy<br />
but allows you to do much more interesting things with those assets. For more information on<br />
using assetic see How to Use Assetic for Asset Management.<br />
Start by adding two blocks to your base template that will hold your assets: one<br />
called stylesheetsinside the head tag and another called javascripts just above the<br />
closing body tag. These blocks will contain all of the stylesheets and Javascripts that you'll need<br />
throughout your site:<br />
{# 'app/Resources/views/base.html.twig' #}<br />
<br />
<br />
{# ... #}<br />
{% block stylesheets %}<br />
<br />
{% endblock %}<br />
<br />
<br />
{# ... #}<br />
{% block javascripts %}<br />
<br />
{% endblock %}<br />
<br />
<br />
That's easy enough! But what if you need to include an extra stylesheet or Javascript from a child<br />
template? For example, suppose you have a contact page and you need to include<br />
a contact.cssstylesheet just on that page. From inside that contact page's template, do the<br />
following:<br />
{# src/Acme/DemoBundle/Resources/views/Contact/contact.html.twig #}<br />
{# extends '::base.html.twig' #}<br />
{% block stylesheets %}<br />
{{ parent() }}<br />
<br />
{% endblock %}<br />
{# ... #}<br />
In the child template, you simply override the stylesheets block and put your new stylesheet<br />
tag inside of that block. Of course, since you want to add to the parent block's content (and not<br />
actuallyreplace it), you should use the parent() Twig function to include everything from<br />
the stylesheetsblock of the base template.<br />
The end result is a page that includes both the main.css and contact.css stylesheets.<br />
77