Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
sneakyvv committed Sep 5, 2023
1 parent af85c5d commit 6f5718c
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/TwigComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,42 @@ When overriding the ``alert_message`` block, you have access to the ``message``
{% endblock %}
{% endcomponent %}
Sometimes, you may want to override a block of a deeply nested component and you need access to
some properties or functions from higher components. That can be done via the ``this.parent...`` hierarchy:

.. code-block:: twig
{# templates/SuccessAlert.html.twig #}
{% component Alert with { type: 'success' } %}
{{ this.parent.someFunction }} {# this refers to SuccessAlert #}
{{ this.parent.someProp }} {# references a "someProp" prop from SuccessAlert #}
{% endcomponent %}
To keep the generic Alert component unaware of the ``someProp`` prop it is better to use ``this.parent``
instead of passing that info along via Alert's attributes.

You can keep referring to 'grand parents' as well. Just add another ``parent``.
Remember though that the parent reference only starts once you're INSIDE the (embedded) component.

.. code-block:: twig
{# templates/FancyProfileCard.html.twig #}
{% component Card %}
{% block header %}
{% component Alert with { message: this.parent.someProp } %} {# not yet INSIDE the Alert template #}
{% block content %}
{{ message }} {# same value as below, indirectly refers to FancyProfileCard::someProp #}
{{ this.parent.parent.someProp }} {# directly refers to FancyProfileCard::someProp #}
{% endblock %}
{% endcomponent %}
{% endblock %}
{% endcomponent %}
.. versionadded:: 2.12

The ability to refer to higher components via the ``this.parent`` syntax was added in 2.12.

Component HTML Syntax
---------------------

Expand Down

0 comments on commit 6f5718c

Please sign in to comment.