@@ -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 ])
@@ -838,25 +838,30 @@ In practice, the ``base.html.twig`` template would look like this:
838838 <head>
839839 <meta charset="UTF-8">
840840 <title>{% block title %}My Application{% endblock %}</title>
841+ {% block stylesheets %}
842+ <link rel="stylesheet" type="text/css" href="/css/base.css"/>
843+ {% endblock
841844 </head>
842845 <body>
843- <div id="sidebar">
844- {% block sidebar %}
845- <ul>
846- <li><a href="{{ path('homepage') }}">Home</a></li>
847- <li><a href="{{ path('blog_index') }}">Blog</a></li>
848- </ul>
849- {% endblock %}
850- </div>
851-
852- <div id="content">
853- {% block body %}{% endblock %}
854- </div>
846+ {% block body %}
847+ <div id="sidebar">
848+ {% block sidebar %}
849+ <ul>
850+ <li><a href="{{ path('homepage') }}">Home</a></li>
851+ <li><a href="{{ path('blog_index') }}">Blog</a></li>
852+ </ul>
853+ {% endblock %}
854+ </div>
855+
856+ <div id="content">
857+ {% block content %}{% endblock %}
858+ </div>
859+ {% endblock %}
855860 </body>
856861 </html>
857862
858863The `Twig block tag `_ defines the page sections that can be overridden in the
859- child templates. They can be empty, like the ``body `` block or define a default
864+ child templates. They can be empty, like the ``content `` block or define a default
860865content, like the ``title `` block, which is displayed when child templates don't
861866override them.
862867
@@ -867,14 +872,14 @@ The ``blog/layout.html.twig`` template could be like this:
867872 {# templates/blog/layout.html.twig #}
868873 {% extends 'base.html.twig' %}
869874
870- {% block body %}
875+ {% block content %}
871876 <h1>Blog</h1>
872877
873- {% block content %}{% endblock %}
878+ {% block page_contents %}{% endblock %}
874879 {% endblock %}
875880
876881The template extends from ``base.html.twig `` and only defines the contents of
877- the ``body `` block. The rest of the parent template blocks will display their
882+ the ``content `` block. The rest of the parent template blocks will display their
878883default contents. However, they can be overridden by the third-level inheritance
879884template, such as ``blog/index.html.twig ``, which displays the blog index:
880885
@@ -885,22 +890,38 @@ template, such as ``blog/index.html.twig``, which displays the blog index:
885890
886891 {% block title %}Blog Index{% endblock %}
887892
888- {% block content %}
893+ {% block page_contents %}
889894 {% for article in articles %}
890895 <h2>{{ article.title }}</h2>
891896 <p>{{ article.body }}</p>
892897 {% endfor %}
893898 {% endblock %}
894899
895900This template extends from the second-level template (``blog/layout.html.twig ``)
896- but overrides blocks of different parent templates: ``content `` from
901+ but overrides blocks of different parent templates: ``page_contents `` from
897902``blog/layout.html.twig `` and ``title `` from ``base.html.twig ``.
898903
899904When you render the ``blog/index.html.twig `` template, Symfony uses three
900905different templates to create the final contents. This inheritance mechanism
901906boosts your productivity because each template includes only its unique contents
902907and leaves the repeated contents and HTML structure to some parent templates.
903908
909+ .. caution ::
910+
911+ When using ``extends ``, a child template is forbidden to define template
912+ parts outside of a block. The following code throws a ``SyntaxError ``:
913+
914+ .. code-block :: html+twig
915+
916+ {# app/Resources/views/blog/index.html.twig #}
917+ {% extends 'base.html.twig' %}
918+
919+ {# the line below is not captured by a "block" tag #}
920+ <div class="alert">Some Alert</div>
921+
922+ {# the following is valid #}
923+ {% block content %}My cool blog posts{% endblock %}
924+
904925Read the `Twig template inheritance `_ docs to learn more about how to reuse
905926parent block contents when overriding templates and other advanced features.
906927
0 commit comments