@@ -178,20 +178,25 @@ First, build a base layout file:
178
178
<head>
179
179
<meta charset="UTF-8">
180
180
<title>{% block title %}Test Application{% endblock %}</title>
181
+ {% block stylesheets %}
182
+ <link rel="stylesheet" type="text/css" href="/css/base.css" />
183
+ {% endblock
181
184
</head>
182
185
<body>
183
- <div id="sidebar">
184
- {% block sidebar %}
185
- <ul>
186
- <li><a href="/">Home</a></li>
187
- <li><a href="/blog">Blog</a></li>
188
- </ul>
189
- {% endblock %}
190
- </div>
191
-
192
- <div id="content">
193
- {% block body %}{% endblock %}
194
- </div>
186
+ {% block body %}
187
+ <div id="sidebar">
188
+ {% block sidebar %}
189
+ <ul>
190
+ <li><a href="/">Home</a></li>
191
+ <li><a href="/blog">Blog</a></li>
192
+ </ul>
193
+ {% endblock %}
194
+ </div>
195
+
196
+ <div id="content">
197
+ {% block content %}{% endblock %}
198
+ </div>
199
+ {% endblock %}
195
200
</body>
196
201
</html>
197
202
@@ -201,11 +206,11 @@ First, build a base layout file:
201
206
the philosophy is the same between Twig and PHP templates.
202
207
203
208
This template defines the base HTML skeleton document of a simple two-column
204
- page. In this example, three ``{% block %} `` areas are defined (``title ``,
205
- ``sidebar `` and ``body ``). Each block may be overridden by a child template
206
- or left with its default implementation. This template could also be rendered
207
- directly. In that case the ``title ``, `` sidebar `` and `` body `` blocks would
208
- simply retain the default values used in this template.
209
+ page. In this example, five ``{% block %} `` areas are defined (``title ``,
210
+ ``stylesheets ``, `` body ``, `` sidebar `` and ``content ``). Each block may be
211
+ overridden by a child template or left with its default implementation. This
212
+ template could also be rendered directly. In that case the ``title `` and
213
+ `` body `` blocks would simply retain the default values used in this template.
209
214
210
215
A child template might look like this:
211
216
@@ -243,28 +248,22 @@ output might look like this:
243
248
<head >
244
249
<meta charset =" UTF-8" >
245
250
<title >My cool blog posts</title >
251
+ <link rel =" stylesheet" type =" text/css" href =" /css/base.css" />
246
252
</head >
247
253
<body >
248
- <div id =" sidebar" >
249
- <ul >
250
- <li ><a href =" /" >Home</a ></li >
251
- <li ><a href =" /blog" >Blog</a ></li >
252
- </ul >
253
- </div >
254
-
255
- <div id =" content" >
256
- <h2 >My first post</h2 >
257
- <p >The body of the first post.</p >
258
-
259
- <h2 >Another post</h2 >
260
- <p >The body of the second post.</p >
261
- </div >
254
+ <h2 >My first post</h2 >
255
+ <p >The body of the first post.</p >
256
+
257
+ <h2 >Another post</h2 >
258
+ <p >The body of the second post.</p >
262
259
</body >
263
260
</html >
264
261
265
- Notice that since the child template didn't define a ``sidebar `` block, the
262
+ Notice that since the child template didn't define a ``stylesheets `` block, the
266
263
value from the parent template is used instead. Content within a ``{% block %} ``
267
264
tag in a parent template is always used by default.
265
+ However the ``body `` block has been replaced entirely. To keep a default sidebar
266
+ the ``content `` block can be overridden instead.
268
267
269
268
.. tip ::
270
269
@@ -301,6 +300,23 @@ When working with template inheritance, here are some tips to keep in mind:
301
300
{{ parent() }}
302
301
{% endblock %}
303
302
303
+ .. caution ::
304
+
305
+ When using ``extends ``, a child template is forbidden to define template
306
+ parts outside of a block. The following code throws a ``SyntaxError ``:
307
+
308
+ .. code-block :: html+twig
309
+
310
+ {# app/Resources/views/blog/index.html.twig #}
311
+ {% extends 'base.html.twig' %}
312
+
313
+ {# the line below is not captured by a "block" tag #}
314
+ <div class="alert">Some Alert
315
+
316
+ {# the following is valid #}
317
+ {% block content %}My cool blog posts{% endblock %}
318
+
319
+
304
320
.. index ::
305
321
single: Templating; Naming conventions
306
322
single: Templating; File locations
0 commit comments