Skip to content

Commit 31c09f3

Browse files
committed
document embedded components
1 parent 6b6f137 commit 31c09f3

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

src/TwigComponent/src/Resources/doc/index.rst

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,73 @@ independently. If you're using `Live Components`_, then there
646646
*are* some guidelines related to how the re-rendering of parent and
647647
child components works. Read `Live Nested Components`_.
648648

649+
Embedded Components
650+
-------------------
651+
652+
.. versionadded:: 2.2
653+
654+
Embedded components were added in TwigComponents 2.2.
655+
656+
You can write your component's Twig template with blocks that can be overridden
657+
when rendering using the ``{% component %}``. These blocks can be thought of as
658+
*slots* which you may be familiar with from Vue. The ``component`` tag is very
659+
similar to Twig's native `embed tag`_.
660+
661+
Consider a data table component. You pass it headers and rows but can expose
662+
blocks for the cells and an optional footer:
663+
664+
.. code-block:: twig
665+
666+
{# templates/components/data_table.html.twig #}
667+
668+
<div{{ attributes.defaults({class: 'data-table'}) }}>
669+
<table>
670+
<thead>
671+
<tr>
672+
{% for header in this.headers %}
673+
<th>
674+
{% block th %}{{ header }}{% endblock %}
675+
</th>
676+
{% endfor %}
677+
</tr>
678+
</thead>
679+
<tbody>
680+
{% for row in this.data %}
681+
<tr>
682+
{% for cell in row %}
683+
<td>
684+
{% block td %}{{ cell }}{% endblock %}
685+
</td>
686+
{% endfor %}
687+
</tr>
688+
{% endfor %}
689+
</tbody>
690+
</table>
691+
{% if block('footer') is defined %}
692+
<div class="data-table-footer">
693+
{{ block('footer') }}
694+
</div>
695+
{% endif %}
696+
</div>
697+
698+
When rendering, you can override the ``th``, ``th`` blocks and add a ``footer`` block.
699+
The ``with`` data is what's mounted on the component object.
700+
701+
.. code-block:: twig
702+
703+
{# templates/some_page.html.twig #}
704+
705+
{% component table with {headers: ['key', 'value'], data: [[1, 2], [3, 4]]} %}
706+
{% block th %}custom th ({{ parent() }}){% endblock %}
707+
{% block td %}custom td ({{ parent() }}){% endblock %}
708+
709+
{% block footer %}My footer{% endblock %}
710+
{% endcomponent %}
711+
712+
.. note::
713+
714+
Embedded components *cannot* currently be used with LiveComponents.
715+
649716
Contributing
650717
------------
651718

@@ -667,3 +734,4 @@ meaning it is not bound to Symfony's BC policy for the moment.
667734
.. _`Vue`: https://v3.vuejs.org/guide/computed.html
668735
.. _`Live Nested Components`: https://symfony.com/bundles/ux-live-component/current/index.html#nested-components
669736
.. _`experimental`: https://symfony.com/doc/current/contributing/code/experimental.html
737+
.. _`embed tag`: https://twig.symfony.com/doc/3.x/tags/embed.html

0 commit comments

Comments
 (0)