Skip to content

Commit 11047c3

Browse files
WebMambaweaverryan
authored andcommitted
Add docs for the new Twig Component syntax
1 parent cc414dc commit 11047c3

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

src/TwigComponent/doc/index.rst

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,86 @@ The ``with`` data is what's mounted on the component object.
840840
.. note::
841841

842842
Embedded components *cannot* currently be used with LiveComponents.
843+
844+
Component HTML Syntax
845+
---------------------
846+
847+
.. versionadded:: 2.8
848+
849+
This syntax was been introduced in 2.8 and is still experimental: it may change in the future.
850+
851+
Twig Components come with an HTML-like syntax to ease the readability of your template:
852+
853+
.. code-block:: html+twig
854+
855+
<twig:Alert></:Alert>
856+
// or use a self-closing tag
857+
<twig:Alert/>
858+
859+
You can pass props to your component by using HTML attributes. Suppose you have the following component:
860+
861+
.. code-block:: html+twig
862+
863+
// "withActions" property will be set to true
864+
<twig:Alert withActions message="hello"></:Alert>
865+
866+
You can add the ':' prefix to your attribute to indicate that the value
867+
should be compiled by Twig
868+
869+
.. code-block:: html+twig
870+
871+
<twig:Alert message="hello" :user="user.id"/>
872+
873+
// equal to
874+
<twig:Alert message="hello" user="{{ user.id }}"/>
875+
876+
// and pass object, or table, or anything you imagine
877+
<twig:Alert :foo="['col' => ['foo', 'oof']]"/>
878+
879+
You can pass content directly inside your component.
880+
881+
.. code-block:: html+twig
882+
883+
<twig:Alert>
884+
// any content you want
885+
<div>
886+
...
887+
</div>
888+
</twig:Alert>
889+
890+
Then in your component template, This becomes a block called content:
891+
892+
.. code-block:: html+twig
893+
894+
<div class="content">
895+
{% block content %}
896+
// and the content will appear in here
897+
{% endblock %}
898+
{% block footer %}
899+
...
900+
{% block footer %}
901+
</div>
902+
903+
In addition to the default block, you can also add named blocks:
904+
905+
.. code-block:: html+twig
906+
907+
<twig:Alert message="hello" :user="user.id">
908+
<twig:block name="footer">
909+
...
910+
</twig:block>
911+
</twig:Alert>
912+
913+
And in your component template you can access your embedded block
914+
915+
.. code-block:: html+twig
916+
917+
<div class="content">
918+
{% block footer %}
919+
...
920+
{% block footer %}
921+
</div>
922+
843923

844924
Contributing
845925
------------

0 commit comments

Comments
 (0)