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