Skip to content

Commit 6691401

Browse files
committed
[Twig] Tweaking docs for new HTML syntax
1 parent 1620116 commit 6691401

File tree

2 files changed

+60
-49
lines changed

2 files changed

+60
-49
lines changed

.doctor-rst.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ rules:
6565
no_php_open_tag_in_code_block_php_directive: ~
6666
# no_php_prefix_before_bin_console: ~
6767
# no_php_prefix_before_composer: ~
68-
no_space_before_self_xml_closing_tag: ~
68+
# no_space_before_self_xml_closing_tag: ~
6969
only_backslashes_in_namespace_in_php_code_block: ~
7070
only_backslashes_in_use_statements_in_php_code_block: ~
7171
ordered_use_statements: ~

src/TwigComponent/doc/index.rst

Lines changed: 59 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -778,9 +778,10 @@ child components works. Read `Live Nested Components`_.
778778
Embedded Components
779779
-------------------
780780

781-
.. versionadded:: 2.2
781+
.. tip::
782782

783-
Embedded components were added in TwigComponents 2.2.
783+
Embedded components (i.e. components with blocks) can be written in a more
784+
readable way by using the `Component HTML Syntax`_.
784785

785786
You can write your component's Twig template with blocks that can be overridden
786787
when rendering using the ``{% component %}`` syntax. These blocks can be thought of as
@@ -840,86 +841,96 @@ The ``with`` data is what's mounted on the component object.
840841
.. note::
841842

842843
Embedded components *cannot* currently be used with LiveComponents.
843-
844+
844845
Component HTML Syntax
845846
---------------------
846-
847+
847848
.. versionadded:: 2.8
848849

849850
This syntax was been introduced in 2.8 and is still experimental: it may change in the future.
850-
851+
851852
Twig Components come with an HTML-like syntax to ease the readability of your template:
852853

853854
.. code-block:: html+twig
854855

855-
<twig:Alert></:Alert>
856+
<twig:Alert></twig:Alert>
856857
// 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:
858+
<twig:Alert />
859+
860+
Passing Props as HTML Attributes
861+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
862+
863+
Passing props is done with HTML attributes. For example if you have this component::
864+
865+
#[AsTwigComponent]
866+
class Alert
867+
{
868+
public string $message = '';
869+
public bool $withActions = false;
870+
public string $type = 'success';
871+
}
872+
873+
You can pass the ``message``, ``withActions`` or ``type`` props as attributes:
860874

861875
.. code-block:: html+twig
862876

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
877+
// withActions will be set to true
878+
<twig:Alert type="info" message="hello!" withActions />
879+
880+
To pass in a dynamic value, prefix the attribute with ``:`` or use the
881+
normal ``{{ }}`` syntax:
868882

869883
.. code-block:: html+twig
870-
871-
<twig:Alert message="hello" :user="user.id"/>
872-
884+
885+
<twig:Alert message="hello!" :user="user.id" />
886+
873887
// equal to
874-
<twig:Alert message="hello" user="{{ user.id }}"/>
875-
888+
<twig:Alert message="hello!" user="{{ user.id }}" />
889+
876890
// 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.
891+
<twig:Alert :foo="['col' => ['foo', 'oof']]" />
892+
893+
Passing Blocks to your Component
894+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
895+
896+
You can also pass content directly to your component:
880897

881898
.. code-block:: html+twig
882899

883-
<twig:Alert>
884-
// any content you want
885-
<div>
886-
...
887-
</div>
900+
<twig:Alert type="success">
901+
<div>Congratulations! You've won a free puppy!</div>
888902
</twig:Alert>
889-
890-
Then in your component template, This becomes a block called content:
903+
904+
In your component template, this becomes a block named ``content``:
891905

892906
.. code-block:: html+twig
893907

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-
908+
<div class="alert alert-{{ type }}">
909+
{% block content %}
910+
// the content will appear in here
911+
{% endblock %}
912+
</div>
913+
903914
In addition to the default block, you can also add named blocks:
904915

905916
.. code-block:: html+twig
906-
907-
<twig:Alert message="hello" :user="user.id">
917+
918+
<twig:Alert type="success">
919+
<div>Congrats on winning a free puppy!</div>
920+
908921
<twig:block name="footer">
909-
...
922+
<button class="btn btn-primary">Claim your prize</button>
910923
</twig:block>
911924
</twig:Alert>
912-
925+
913926
And in your component template you can access your embedded block
914927

915928
.. code-block:: html+twig
916-
917-
<div class="content">
918-
{% block footer %}
919-
...
920-
{% block footer %}
929+
930+
<div class="alert alert-{{ type }}">
931+
{% block content %}{% endblock %}
932+
{% block footer %}{% endblock %}
921933
</div>
922-
923934

924935
Contributing
925936
------------

0 commit comments

Comments
 (0)