@@ -778,9 +778,10 @@ child components works. Read `Live Nested Components`_.
778
778
Embedded Components
779
779
-------------------
780
780
781
- .. versionadded :: 2.2
781
+ .. tip ::
782
782
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 `_.
784
785
785
786
You can write your component's Twig template with blocks that can be overridden
786
787
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.
840
841
.. note ::
841
842
842
843
Embedded components *cannot * currently be used with LiveComponents.
843
-
844
+
844
845
Component HTML Syntax
845
846
---------------------
846
-
847
+
847
848
.. versionadded :: 2.8
848
849
849
850
This syntax was been introduced in 2.8 and is still experimental: it may change in the future.
850
-
851
+
851
852
Twig Components come with an HTML-like syntax to ease the readability of your template:
852
853
853
854
.. code-block :: html+twig
854
855
855
- <twig:Alert></:Alert>
856
+ <twig:Alert></twig :Alert>
856
857
// 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:
860
874
861
875
.. code-block :: html+twig
862
876
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:
868
882
869
883
.. code-block :: html+twig
870
-
871
- <twig:Alert message="hello" :user="user.id"/>
872
-
884
+
885
+ <twig:Alert message="hello! " :user="user.id" />
886
+
873
887
// equal to
874
- <twig:Alert message="hello" user="{{ user.id }}"/>
875
-
888
+ <twig:Alert message="hello! " user="{{ user.id }}" />
889
+
876
890
// 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:
880
897
881
898
.. code-block :: html+twig
882
899
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>
888
902
</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 `` :
891
905
892
906
.. code-block :: html+twig
893
907
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
+
903
914
In addition to the default block, you can also add named blocks:
904
915
905
916
.. 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
+
908
921
<twig:block name="footer">
909
- ...
922
+ <button class="btn btn-primary">Claim your prize</button>
910
923
</twig:block>
911
924
</twig:Alert>
912
-
925
+
913
926
And in your component template you can access your embedded block
914
927
915
928
.. 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 %}
921
933
</div>
922
-
923
934
924
935
Contributing
925
936
------------
0 commit comments