@@ -841,8 +841,10 @@ Inheritance & Forwarding "Outer Blocks"
841
841
842
842
The ``outerBlocks `` variable was added in 2.10.
843
843
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:
846
848
847
849
.. code-block :: html+twig
848
850
@@ -856,14 +858,17 @@ means to refer to blocks defined in the same template:
856
858
{% endcomponent %}
857
859
{% endblock %}
858
860
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:
861
863
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 %}
865
870
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 :
867
872
868
873
.. code-block :: html+twig
869
874
@@ -872,43 +877,26 @@ This has a few interesting side effects. For example, imagine a simple ``Alert``
872
877
{% block content %}{% endblock %}
873
878
</div>
874
879
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 ``:
887
882
888
883
.. code-block :: twig
889
884
890
885
{# templates/SuccessAlert.html.twig #}
891
886
{% 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 %}
900
888
{% endcomponent %}
901
889
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.
903
891
904
892
Context / Variables Inside of Blocks
905
893
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
906
894
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,
908
896
independent template, which extends the component's template. This has a few interesting consequences.
909
897
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:
912
900
913
901
.. code-block :: twig
914
902
@@ -921,7 +909,7 @@ the component you're now rendering and you have access to all of that component'
921
909
{{ type }} {# references a "type" prop from Alert #}
922
910
{% endcomponent %}
923
911
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
925
913
access to whatever variables are available in the original template:
926
914
927
915
.. code-block :: twig
@@ -934,12 +922,12 @@ access to whatever variables are available in the original template:
934
922
935
923
Note that ALL variables from upper components (e.g. ``SuccessAlert ``) are available to lower
936
924
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).
939
927
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
941
929
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:
943
931
944
932
.. code-block :: twig
945
933
0 commit comments