Skip to content

Commit acaa41a

Browse files
authored
proofing
1 parent ae5a458 commit acaa41a

File tree

1 file changed

+25
-37
lines changed

1 file changed

+25
-37
lines changed

src/TwigComponent/doc/index.rst

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -841,8 +841,10 @@ Inheritance & Forwarding "Outer Blocks"
841841

842842
The ``outerBlocks`` variable was added in 2.10.
843843

844-
Consider this simple example to explain the basic usage of ``outerBlocks`` which serves as a
845-
means to refer to blocks defined in the same template:
844+
The content inside a ``{% component ... %}`` tag should be viewed as living in
845+
its own, independent template, which extends the component's template. This means that
846+
any blocks that live in the "outer" template are not available inside the ``{% component %}`` tag.
847+
However, a special ``outerBlocks`` variable is added as a way to refer to those blocks:
846848

847849
.. code-block:: html+twig
848850

@@ -856,14 +858,17 @@ means to refer to blocks defined in the same template:
856858
{% endcomponent %}
857859
{% endblock %}
858860

859-
Although this is not a super practical example. The ``outerBlocks`` variable becomes a lot
860-
more useful with nested components.
861+
The ``outerBlocks`` variable becomes specially useful with nested components. For example,
862+
imagine we want to create a ``SuccessAlert`` component that's usable like this:
861863

862-
When passing blocks via the ``{% component %}`` syntax, there is one important thing
863-
to understand: the content behaves as if it lives in its **own**, independent template,
864-
which extends the component's template.
864+
.. code-block:: twig
865+
866+
{# templates/some_page.html.twig #}
867+
{% component SuccessAlert %}
868+
{% content %}We will successfully <em>forward</em> this block content!{% endblock %}
869+
{% endcomponent %}
865870
866-
This has a few interesting side effects. For example, imagine a simple ``Alert`` component:
871+
But we already have a generic ``Alert`` component, and we want to re-use it:
867872

868873
.. code-block:: html+twig
869874

@@ -872,43 +877,26 @@ This has a few interesting side effects. For example, imagine a simple ``Alert``
872877
{% block content %}{% endblock %}
873878
</div>
874879

875-
And a ``SuccessAlert`` component that uses it:
876-
877-
.. code-block:: twig
878-
879-
{# templates/SuccessAlert.html.twig #}
880-
{% component Alert with { type: 'success' } %}
881-
I can override the content, but that's pretty dull.
882-
{% endcomponent %}
883-
884-
What if this SuccessAlert component wants to make the content of its nested Alert component configurable?
885-
Well, you can use the special ``outerBlocks`` variable for those cases. This makes it possible to refer
886-
to blocks that are passed in from another higher component.
880+
To do this, the ``SuccessAlert`` component can grab the ``content`` block that's passed to it
881+
via the ``outerBlocks`` variable and forward it into ``Alert``:
887882

888883
.. code-block:: twig
889884
890885
{# templates/SuccessAlert.html.twig #}
891886
{% component Alert with { type: 'success' } %}
892-
{{ blocks(outerBlocks.content) }}
893-
{% endcomponent %}
894-
895-
.. code-block:: twig
896-
897-
{# templates/some_page.html.twig #}
898-
{% component SuccessAlert %}
899-
Look mom, I'm a complex component!
887+
{% block content %}{{ blocks(outerBlocks.content) }}{% endblock %}
900888
{% endcomponent %}
901889
902-
Note that to pass a block multiple components down, each component would need to pass it along.
890+
Note that to pass a block multiple components down, each component needs to pass it.
903891

904892
Context / Variables Inside of Blocks
905893
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
906894

907-
The content inside of the ``{% block component %}`` should be viewed as living in its own,
895+
The content inside of the ``{% component ... %}`` should be viewed as living in its own,
908896
independent template, which extends the component's template. This has a few interesting consequences.
909897

910-
First, once you're inside of ``{% block component %}``, the ``this`` variable represents
911-
the component you're now rendering and you have access to all of that component's variables:
898+
First, once you're inside of ``{% component ... %}``, the ``this`` variable represents
899+
the component you're now rendering *and* you have access to all of that component's variables:
912900

913901
.. code-block:: twig
914902
@@ -921,7 +909,7 @@ the component you're now rendering and you have access to all of that component'
921909
{{ type }} {# references a "type" prop from Alert #}
922910
{% endcomponent %}
923911
924-
Conveniently, in addition to the variables from the `Alert` component, you still have
912+
Conveniently, in addition to the variables from the ``Alert`` component, you still have
925913
access to whatever variables are available in the original template:
926914

927915
.. code-block:: twig
@@ -934,12 +922,12 @@ access to whatever variables are available in the original template:
934922
935923
Note that ALL variables from upper components (e.g. ``SuccessAlert``) are available to lower
936924
components (e.g. ``Alert``). However, because variables are merged, variables with the same name
937-
are overridden by lower components. (That's also why ``this`` refers to the embedded, or
938-
"current" component)
925+
are overridden by lower components (that's also why ``this`` refers to the embedded, or
926+
"current" component).
939927

940-
Finally, the most interesting thing is that the content inside of ``{% component %}`` is
928+
The most interesting thing is that the content inside of ``{% component ... %}`` is
941929
executed as if it is "copy-and-pasted" into the block of the target template. This means
942-
you can access variables from the block you're overriding. For example:
930+
you can access variables from the block you're overriding! For example:
943931

944932
.. code-block:: twig
945933

0 commit comments

Comments
 (0)