@@ -1085,43 +1085,76 @@ one called ``stylesheets`` inside the ``head`` tag and another called ``javascri
10851085just above the closing ``body `` tag. These blocks will contain all of the
10861086stylesheets and JavaScripts that you'll need throughout your site:
10871087
1088- .. code -block :: html+jinja
1088+ .. configuration -block ::
10891089
1090- {# app/Resources/views/base.html.twig #}
1091- <html>
1092- <head>
1093- {# ... #}
1090+ .. code-block :: html+jinja
10941091
1095- {% block stylesheets %}
1096- <link href="{{ asset('css/main.css') }}" rel="stylesheet" />
1097- {% endblock %}
1098- </head>
1099- <body>
1100- {# ... #}
1092+ {# app/Resources/views/base.html.twig #}
1093+ <html>
1094+ <head>
1095+ {# ... #}
11011096
1102- {% block javascripts %}
1103- <script src="{{ asset('js/main.js') }}"></script>
1104- {% endblock %}
1105- </body>
1106- </html>
1097+ {% block stylesheets %}
1098+ <link href="{{ asset('css/main.css') }}" rel="stylesheet" />
1099+ {% endblock %}
1100+ </head>
1101+ <body>
1102+ {# ... #}
1103+
1104+ {% block javascripts %}
1105+ <script src="{{ asset('js/main.js') }}"></script>
1106+ {% endblock %}
1107+ </body>
1108+ </html>
1109+
1110+ .. code-block :: php
1111+
1112+ // app/Resources/views/base.html.php
1113+ <html >
1114+ <head >
1115+ <?php ... ?>
1116+
1117+ <?php $view['slots']->start('stylesheets') ?>
1118+ <link href =" <?php echo $view['assets']->getUrl('css/main.css') ?>" rel =" stylesheet" />
1119+ <?php $view['slots']->stop() ?>
1120+ </head >
1121+ <body >
1122+ <?php ... ?>
1123+
1124+ <?php $view['slots']->start('javascripts') ?>
1125+ <script src =" <?php echo $view['assets']->getUrl('js/main.js') ?>" ></script >
1126+ <?php $view['slots']->stop() ?>
1127+ </body >
1128+ </html >
11071129
11081130 That's easy enough! But what if you need to include an extra stylesheet or
11091131JavaScript from a child template? For example, suppose you have a contact
11101132page and you need to include a ``contact.css `` stylesheet *just * on that
11111133page. From inside that contact page's template, do the following:
11121134
1113- .. code-block :: html+jinja
1135+ .. configuration-block ::
1136+
1137+ .. code-block :: html+jinja
1138+
1139+ {# app/Resources/views/Contact/contact.html.twig #}
1140+ {% extends 'base.html.twig' %}
1141+
1142+ {% block stylesheets %}
1143+ {{ parent() }}
1144+
1145+ <link href="{{ asset('css/contact.css') }}" rel="stylesheet" />
1146+ {% endblock %}
11141147
1115- {# app/Resources/views/Contact/contact.html.twig #}
1116- {% extends 'base.html.twig' %}
1148+ {# ... #}
11171149
1118- {% block stylesheets %}
1119- {{ parent() }}
1150+ .. code-block :: php
11201151
1121- <link href="{{ asset('css/ contact.css') }}" rel="stylesheet" />
1122- {% endblock %}
1152+ // app/Resources/views/Contact/ contact.html.twig
1153+ <?php $view->extend('base.html.php') ?>
11231154
1124- {# ... #}
1155+ <?php $view['slots']->start('stylesheets') ?>
1156+ <link href =" <?php echo $view['assets']->getUrl('css/contact.css') ?>" rel =" stylesheet" />
1157+ <?php $view['slots']->stop() ?>
11251158
11261159 In the child template, you simply override the ``stylesheets `` block and
11271160put your new stylesheet tag inside of that block. Of course, since you want
0 commit comments