Skip to content

Commit d8ca2ef

Browse files
committed
Add documentation
1 parent d98b0cc commit d8ca2ef

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/TwigComponent/doc/index.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,42 @@ When overriding the ``alert_message`` block, you have access to the ``message``
951951
{% endblock %}
952952
{% endcomponent %}
953953
954+
Sometimes, you may want to override a block of a deeply nested component and you need access to
955+
some properties or functions from higher components. That can be done via the ``this.parent...`` hierarchy:
956+
957+
.. code-block:: twig
958+
959+
{# templates/SuccessAlert.html.twig #}
960+
{% component Alert with { type: 'success' } %}
961+
{{ this.parent.someFunction }} {# this refers to SuccessAlert #}
962+
963+
{{ this.parent.someProp }} {# references a "someProp" prop from SuccessAlert #}
964+
{% endcomponent %}
965+
966+
To keep the generic Alert component unaware of the ``someProp`` prop it is better to use ``this.parent``
967+
instead of passing that info along via Alert's attributes.
968+
969+
You can keep referring to 'grand parents' as well. Just add another ``parent``.
970+
Remember though that the parent reference only starts once you're INSIDE the (embedded) component.
971+
972+
.. code-block:: twig
973+
974+
{# templates/FancyProfileCard.html.twig #}
975+
{% component Card %}
976+
{% block header %}
977+
{% component Alert with { message: this.parent.someProp } %} {# not yet INSIDE the Alert template #}
978+
{% block content %}
979+
{{ message }} {# same value as below, indirectly refers to FancyProfileCard::someProp #}
980+
{{ this.parent.parent.someProp }} {# directly refers to FancyProfileCard::someProp #}
981+
{% endblock %}
982+
{% endcomponent %}
983+
{% endblock %}
984+
{% endcomponent %}
985+
986+
.. versionadded:: 2.12
987+
988+
The ability to refer to higher components via the ``this.parent`` syntax was added in 2.12.
989+
954990
Component HTML Syntax
955991
---------------------
956992

0 commit comments

Comments
 (0)