diff --git a/templating.rst b/templating.rst index 8652b6da136..600648f2f5f 100644 --- a/templating.rst +++ b/templating.rst @@ -178,20 +178,25 @@ First, build a base layout file: {% block title %}Test Application{% endblock %} + {% block stylesheets %} + + {% endblock - - -
- {% block body %}{% endblock %} -
+ {% block body %} + + +
+ {% block content %}{% endblock %} +
+ {% endblock %} @@ -201,11 +206,11 @@ First, build a base layout file: the philosophy is the same between Twig and PHP templates. This template defines the base HTML skeleton document of a simple two-column -page. In this example, three ``{% block %}`` areas are defined (``title``, -``sidebar`` and ``body``). Each block may be overridden by a child template -or left with its default implementation. This template could also be rendered -directly. In that case the ``title``, ``sidebar`` and ``body`` blocks would -simply retain the default values used in this template. +page. In this example, five ``{% block %}`` areas are defined (``title``, +``stylesheets``, ``body``, ``sidebar`` and ``content``). Each block may be +overridden by a child template or left with its default implementation. This +template could also be rendered directly. In that case the ``title`` and +``body`` blocks would simply retain the default values used in this template. A child template might look like this: @@ -243,28 +248,22 @@ output might look like this: My cool blog posts + - - -
-

My first post

-

The body of the first post.

- -

Another post

-

The body of the second post.

-
+

My first post

+

The body of the first post.

+ +

Another post

+

The body of the second post.

-Notice that since the child template didn't define a ``sidebar`` block, the +Notice that since the child template didn't define a ``stylesheets`` block, the value from the parent template is used instead. Content within a ``{% block %}`` tag in a parent template is always used by default. +However the ``body`` block has been replaced entirely. To keep a default sidebar +the ``content`` block can be overridden instead. .. tip:: @@ -301,6 +300,23 @@ When working with template inheritance, here are some tips to keep in mind: {{ parent() }} {% endblock %} +.. caution:: + + When using ``extends``, a child template is forbidden to define template + parts outside of a block. The following code throws a ``SyntaxError``: + + .. code-block:: html+twig + + {# app/Resources/views/blog/index.html.twig #} + {% extends 'base.html.twig' %} + + {# the line below is not captured by a "block" tag #} +
Some Alert
+ + {# the following is valid #} + {% block content %}My cool blog posts{% endblock %} + + .. index:: single: Templating; Naming conventions single: Templating; File locations