@@ -476,9 +476,9 @@ provided by Symfony:
476
476
# special options defined by Symfony to set the page cache
477
477
maxAge : 86400
478
478
sharedAge : 86400
479
-
479
+
480
480
# whether or not caching should apply for client caches only
481
- private : true
481
+ private : true
482
482
483
483
.. code-block :: xml
484
484
@@ -518,7 +518,7 @@ provided by Symfony:
518
518
// special options defined by Symfony to set the page cache
519
519
'maxAge' => 86400,
520
520
'sharedAge' => 86400,
521
-
521
+
522
522
// whether or not caching should apply for client caches only
523
523
'private' => true,
524
524
])
@@ -844,25 +844,30 @@ In practice, the ``base.html.twig`` template would look like this:
844
844
<head>
845
845
<meta charset="UTF-8">
846
846
<title>{% block title %}My Application{% endblock %}</title>
847
+ {% block stylesheets %}
848
+ <link rel="stylesheet" type="text/css" href="/css/base.css"/>
849
+ {% endblock
847
850
</head>
848
851
<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 %}
861
866
</body>
862
867
</html>
863
868
864
869
The `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
866
871
content, like the ``title `` block, which is displayed when child templates don't
867
872
override them.
868
873
@@ -873,14 +878,14 @@ The ``blog/layout.html.twig`` template could be like this:
873
878
{# templates/blog/layout.html.twig #}
874
879
{% extends 'base.html.twig' %}
875
880
876
- {% block body %}
881
+ {% block content %}
877
882
<h1>Blog</h1>
878
883
879
- {% block content %}{% endblock %}
884
+ {% block page_contents %}{% endblock %}
880
885
{% endblock %}
881
886
882
887
The 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
884
889
default contents. However, they can be overridden by the third-level inheritance
885
890
template, such as ``blog/index.html.twig ``, which displays the blog index:
886
891
@@ -891,22 +896,38 @@ template, such as ``blog/index.html.twig``, which displays the blog index:
891
896
892
897
{% block title %}Blog Index{% endblock %}
893
898
894
- {% block content %}
899
+ {% block page_contents %}
895
900
{% for article in articles %}
896
901
<h2>{{ article.title }}</h2>
897
902
<p>{{ article.body }}</p>
898
903
{% endfor %}
899
904
{% endblock %}
900
905
901
906
This 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
903
908
``blog/layout.html.twig `` and ``title `` from ``base.html.twig ``.
904
909
905
910
When you render the ``blog/index.html.twig `` template, Symfony uses three
906
911
different templates to create the final contents. This inheritance mechanism
907
912
boosts your productivity because each template includes only its unique contents
908
913
and leaves the repeated contents and HTML structure to some parent templates.
909
914
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
+
910
931
Read the `Twig template inheritance `_ docs to learn more about how to reuse
911
932
parent block contents when overriding templates and other advanced features.
912
933
0 commit comments