@@ -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
])
@@ -838,25 +838,30 @@ In practice, the ``base.html.twig`` template would look like this:
838
838
<head>
839
839
<meta charset="UTF-8">
840
840
<title>{% block title %}My Application{% endblock %}</title>
841
+ {% block stylesheets %}
842
+ <link rel="stylesheet" type="text/css" href="/css/base.css"/>
843
+ {% endblock
841
844
</head>
842
845
<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 %}
855
860
</body>
856
861
</html>
857
862
858
863
The `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
860
865
content, like the ``title `` block, which is displayed when child templates don't
861
866
override them.
862
867
@@ -867,14 +872,14 @@ The ``blog/layout.html.twig`` template could be like this:
867
872
{# templates/blog/layout.html.twig #}
868
873
{% extends 'base.html.twig' %}
869
874
870
- {% block body %}
875
+ {% block content %}
871
876
<h1>Blog</h1>
872
877
873
- {% block content %}{% endblock %}
878
+ {% block page_contents %}{% endblock %}
874
879
{% endblock %}
875
880
876
881
The 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
878
883
default contents. However, they can be overridden by the third-level inheritance
879
884
template, such as ``blog/index.html.twig ``, which displays the blog index:
880
885
@@ -885,22 +890,38 @@ template, such as ``blog/index.html.twig``, which displays the blog index:
885
890
886
891
{% block title %}Blog Index{% endblock %}
887
892
888
- {% block content %}
893
+ {% block page_contents %}
889
894
{% for article in articles %}
890
895
<h2>{{ article.title }}</h2>
891
896
<p>{{ article.body }}</p>
892
897
{% endfor %}
893
898
{% endblock %}
894
899
895
900
This 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
897
902
``blog/layout.html.twig `` and ``title `` from ``base.html.twig ``.
898
903
899
904
When you render the ``blog/index.html.twig `` template, Symfony uses three
900
905
different templates to create the final contents. This inheritance mechanism
901
906
boosts your productivity because each template includes only its unique contents
902
907
and leaves the repeated contents and HTML structure to some parent templates.
903
908
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
+
904
925
Read the `Twig template inheritance `_ docs to learn more about how to reuse
905
926
parent block contents when overriding templates and other advanced features.
906
927
0 commit comments