@@ -1053,10 +1053,9 @@ And in your component template you can access your embedded block
1053
1053
Anonymous TwigComponent
1054
1054
-----------------------
1055
1055
1056
- Sometimes, reusable elements do not require complex logic or injected service to render what it could be processed
1057
- from one template itself. (e.g. retrieving a customized primary button that could take a different label).
1058
-
1059
- No need for class. Your component matches the namespace of the file you created where components live (see `Twig Template Namespaces `_).
1056
+ Sometimes a component is simple enough that it doesn't have any complex logic or injected services.
1057
+ In this case, you can skip the class and only create the template. The component name is determined
1058
+ by the location of the template (see `Twig Template Namespaces `_):
1060
1059
1061
1060
.. code-block :: html+twig
1062
1061
@@ -1066,7 +1065,7 @@ No need for class. Your component matches the namespace of the file you created
1066
1065
{% endblock %}
1067
1066
</button>
1068
1067
1069
- Then use your component with ``: `` to navigate through the directories:
1068
+ Then use your component with ``: `` to navigate through sub- directories (if there are any) :
1070
1069
1071
1070
.. code-block :: html+twig
1072
1071
@@ -1075,54 +1074,84 @@ Then use your component with ``:`` to navigate through the directories:
1075
1074
<div>
1076
1075
<twig:Button: Primary>
1077
1076
Click Me!
1078
- </twig:primary >
1077
+ </twig:Button: Primary >
1079
1078
</div>
1080
1079
1081
- Now let's pass props to your button by defining a ``{% props %} `` tag in its view.
1082
- It allows to declare which props are required and which have a default value.
1083
-
1084
- For a required prop "label", and one optional prop "primary":
1080
+ Still get all your attributes by rendering the ``attributes `` object in your template:
1085
1081
1086
1082
.. code-block :: html+twig
1087
1083
1088
1084
{# templates/components/Button.html.twig #}
1089
- {% props label, primary = true %}
1090
-
1091
- <button class="{{ primary ? 'primary' : 'secondary' }} large rounded">
1092
- {{ label }}
1085
+ <button {{ attributes }}>
1086
+ {% block content %}
1087
+ {% endblock %}
1093
1088
</button>
1094
1089
1095
- Then use it :
1090
+ Then pass any further data to your reusable "button" like so :
1096
1091
1097
1092
.. code-block :: html+twig
1098
1093
1099
1094
{# index.html.twig #}
1100
1095
...
1101
1096
<div>
1102
- <twig:Button label="Click Me!" />
1097
+ <twig:Button role="button" class="foo">
1098
+ Click Me!
1099
+ </twig:Button>
1103
1100
</div>
1104
1101
1105
- You can also define your component to allow overwriting attributes:
1102
+ {# renders as: #}
1103
+ <button role="button" class="foo">Click Me!</button>
1104
+
1105
+ Now let's pass props to your "button" by defining a ``{% props %} `` tag in its view.
1106
+ It allows to declare which props are required and which have a default value.
1107
+
1108
+ For a required prop "label", and one optional prop "primary":
1106
1109
1107
1110
.. code-block :: html+twig
1108
1111
1109
1112
{# templates/components/Button.html.twig #}
1110
1113
{% props label, primary = true %}
1111
1114
1112
- <button {{ attributes.defaults({class: primary ? 'primary' : 'secondary'}) }}>
1115
+ <button class=" {{ primary ? 'primary' : 'secondary' }} large rounded" >
1113
1116
{{ label }}
1114
1117
</button>
1115
1118
1116
- And use it like so :
1119
+ And use it:
1117
1120
1118
1121
.. code-block :: html+twig
1119
1122
1120
1123
{# index.html.twig #}
1121
1124
...
1122
1125
<div>
1123
- <twig:Button label="Click Me!" :primary="false " />
1126
+ <twig:Button label="Click Me!" />
1124
1127
</div>
1125
1128
1129
+ .. note ::
1130
+
1131
+ Definining a property in ``{% props %} `` makes it accessible as a variable and not available inside the ``attributes `` object once passed to the component.
1132
+
1133
+ .. code-block :: html+twig
1134
+
1135
+ {# templates/components/Button.html.twig #}
1136
+ {% props label, disabled = false %}
1137
+
1138
+ <button {{ attributes.defaults({class: disabled ? 'disabled' : 'default'}) }}>
1139
+ {{ label }}
1140
+ </button>
1141
+
1142
+ This means that ``disabled `` is used here to set a class name but will not be rendered in HTML attributes:
1143
+
1144
+ .. code-block :: html+twig
1145
+
1146
+ {# index.html.twig #}
1147
+ ...
1148
+ <div>
1149
+ <twig:Button label="Click Me!" :disabled="true" />
1150
+ </div>
1151
+
1152
+ {# renders as: #}
1153
+ <button class="disabled">Click Me!</button>
1154
+
1126
1155
Test Helpers
1127
1156
------------
1128
1157
0 commit comments