Skip to content

Commit 844dd8a

Browse files
committed
minor #13183 [Twig] tweaked first block examples (HeahDude)
This PR was merged into the 3.4 branch. Discussion ---------- [Twig] tweaked first block examples Here's an attempt to warn about common mistakes when beginning to learn about Twig. Commits ------- d4c44d0 [Twig] tweaked first block examples
2 parents 733aeaf + d4c44d0 commit 844dd8a

File tree

1 file changed

+48
-32
lines changed

1 file changed

+48
-32
lines changed

templating.rst

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -178,20 +178,25 @@ First, build a base layout file:
178178
<head>
179179
<meta charset="UTF-8">
180180
<title>{% block title %}Test Application{% endblock %}</title>
181+
{% block stylesheets %}
182+
<link rel="stylesheet" type="text/css" href="/css/base.css"/>
183+
{% endblock
181184
</head>
182185
<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 %}
195200
</body>
196201
</html>
197202

@@ -201,11 +206,11 @@ First, build a base layout file:
201206
the philosophy is the same between Twig and PHP templates.
202207

203208
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.
209214

210215
A child template might look like this:
211216

@@ -243,28 +248,22 @@ output might look like this:
243248
<head>
244249
<meta charset="UTF-8">
245250
<title>My cool blog posts</title>
251+
<link rel="stylesheet" type="text/css" href="/css/base.css"/>
246252
</head>
247253
<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>
262259
</body>
263260
</html>
264261

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
266263
value from the parent template is used instead. Content within a ``{% block %}``
267264
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.
268267

269268
.. tip::
270269

@@ -301,6 +300,23 @@ When working with template inheritance, here are some tips to keep in mind:
301300
{{ parent() }}
302301
{% endblock %}
303302

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</div>
315+
316+
{# the following is valid #}
317+
{% block content %}My cool blog posts{% endblock %}
318+
319+
304320
.. index::
305321
single: Templating; Naming conventions
306322
single: Templating; File locations

0 commit comments

Comments
 (0)