@@ -476,9 +476,9 @@ provided by Symfony:
476476 # special options defined by Symfony to set the page cache
477477 maxAge : 86400
478478 sharedAge : 86400
479-
479+
480480 # whether or not caching should apply for client caches only
481- private : true
481+ private : true
482482
483483 .. code-block :: xml
484484
@@ -518,7 +518,7 @@ provided by Symfony:
518518 // special options defined by Symfony to set the page cache
519519 'maxAge' => 86400,
520520 'sharedAge' => 86400,
521-
521+
522522 // whether or not caching should apply for client caches only
523523 'private' => true,
524524 ])
@@ -844,25 +844,30 @@ In practice, the ``base.html.twig`` template would look like this:
844844 <head>
845845 <meta charset="UTF-8">
846846 <title>{% block title %}My Application{% endblock %}</title>
847+ {% block stylesheets %}
848+ <link rel="stylesheet" type="text/css" href="/css/base.css"/>
849+ {% endblock
847850 </head>
848851 <body>
849- <div id="sidebar">
850- {% block sidebar %}
851- <ul>
852- <li><a href="{{ path('homepage') }}">Home</a></li>
853- <li><a href="{{ path('blog_index') }}">Blog</a></li>
854- </ul>
855- {% endblock %}
856- </div>
857-
858- <div id="content">
859- {% block body %}{% endblock %}
860- </div>
852+ {% block body %}
853+ <div id="sidebar">
854+ {% block sidebar %}
855+ <ul>
856+ <li><a href="{{ path('homepage') }}">Home</a></li>
857+ <li><a href="{{ path('blog_index') }}">Blog</a></li>
858+ </ul>
859+ {% endblock %}
860+ </div>
861+
862+ <div id="content">
863+ {% block content %}{% endblock %}
864+ </div>
865+ {% endblock %}
861866 </body>
862867 </html>
863868
864869The `Twig block tag `_ defines the page sections that can be overridden in the
865- child templates. They can be empty, like the ``body `` block or define a default
870+ child templates. They can be empty, like the ``content `` block or define a default
866871content, like the ``title `` block, which is displayed when child templates don't
867872override them.
868873
@@ -873,14 +878,14 @@ The ``blog/layout.html.twig`` template could be like this:
873878 {# templates/blog/layout.html.twig #}
874879 {% extends 'base.html.twig' %}
875880
876- {% block body %}
881+ {% block content %}
877882 <h1>Blog</h1>
878883
879- {% block content %}{% endblock %}
884+ {% block page_contents %}{% endblock %}
880885 {% endblock %}
881886
882887The template extends from ``base.html.twig `` and only defines the contents of
883- the ``body `` block. The rest of the parent template blocks will display their
888+ the ``content `` block. The rest of the parent template blocks will display their
884889default contents. However, they can be overridden by the third-level inheritance
885890template, such as ``blog/index.html.twig ``, which displays the blog index:
886891
@@ -891,22 +896,38 @@ template, such as ``blog/index.html.twig``, which displays the blog index:
891896
892897 {% block title %}Blog Index{% endblock %}
893898
894- {% block content %}
899+ {% block page_contents %}
895900 {% for article in articles %}
896901 <h2>{{ article.title }}</h2>
897902 <p>{{ article.body }}</p>
898903 {% endfor %}
899904 {% endblock %}
900905
901906This template extends from the second-level template (``blog/layout.html.twig ``)
902- but overrides blocks of different parent templates: ``content `` from
907+ but overrides blocks of different parent templates: ``page_contents `` from
903908``blog/layout.html.twig `` and ``title `` from ``base.html.twig ``.
904909
905910When you render the ``blog/index.html.twig `` template, Symfony uses three
906911different templates to create the final contents. This inheritance mechanism
907912boosts your productivity because each template includes only its unique contents
908913and leaves the repeated contents and HTML structure to some parent templates.
909914
915+ .. caution ::
916+
917+ When using ``extends ``, a child template is forbidden to define template
918+ parts outside of a block. The following code throws a ``SyntaxError ``:
919+
920+ .. code-block :: html+twig
921+
922+ {# app/Resources/views/blog/index.html.twig #}
923+ {% extends 'base.html.twig' %}
924+
925+ {# the line below is not captured by a "block" tag #}
926+ <div class="alert">Some Alert</div>
927+
928+ {# the following is valid #}
929+ {% block content %}My cool blog posts{% endblock %}
930+
910931Read the `Twig template inheritance `_ docs to learn more about how to reuse
911932parent block contents when overriding templates and other advanced features.
912933
0 commit comments