@@ -1050,8 +1050,8 @@ And in your component template you can access your embedded block
1050
1050
{% block footer %}{% endblock %}
1051
1051
</div>
1052
1052
1053
- Anonymous TwigComponent
1054
- -----------------------
1053
+ Anonymous Components
1054
+ --------------------
1055
1055
1056
1056
Sometimes a component is simple enough that it doesn't have any complex logic or injected services.
1057
1057
In this case, you can skip the class and only create the template. The component name is determined
@@ -1060,97 +1060,59 @@ by the location of the template (see `Twig Template Namespaces`_):
1060
1060
.. code-block :: html+twig
1061
1061
1062
1062
{# templates/components/Button/Primary.html.twig #}
1063
- <button class="primary">
1064
- {% block content %}
1065
- {% endblock %}
1063
+ <button {{ attributes.defaults({ class: 'primary' }) }}>
1064
+ {% block content %}{% endblock %}
1066
1065
</button>
1067
1066
1068
1067
Then use your component with ``: `` to navigate through sub-directories (if there are any):
1069
1068
1070
1069
.. code-block :: html+twig
1071
1070
1072
- {# index.html.twig #}
1073
- ...
1071
+ {# index.html.twig #}
1072
+ ...
1074
1073
<div>
1075
- <twig:Button: Primary>
1076
- Click Me!
1077
- </twig:Button: Primary>
1074
+ <twig:Button: Primary>Click Me!</twig:Button: Primary>
1078
1075
</div>
1079
1076
1080
- Still get all your attributes by rendering the ``attributes `` object in your template:
1081
-
1082
- .. code-block :: html+twig
1083
-
1084
- {# templates/components/Button.html.twig #}
1085
- <button {{ attributes }}>
1086
- {% block content %}
1087
- {% endblock %}
1088
- </button>
1077
+ {# renders as: #}
1078
+ <button class="primary">Click Me!</button>
1089
1079
1090
- Then pass any further data to your reusable "button" like so :
1080
+ Like normal, you can pass extra attributes that will be rendered on the element :
1091
1081
1092
1082
.. code-block :: html+twig
1093
1083
1094
- {# index.html.twig #}
1095
- ...
1084
+ {# index.html.twig #}
1085
+ ...
1096
1086
<div>
1097
- <twig:Button role="button" class="foo">
1098
- Click Me!
1099
- </twig:Button>
1087
+ <twig:Button: Primary type="button" name="foo">Click Me!</twig:Button: Primary>
1100
1088
</div>
1101
1089
1102
1090
{# 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.
1091
+ <button class="primary" type="button" name="foo">Click Me!</button>
1107
1092
1108
- For a required prop "label", and one optional prop "primary" :
1093
+ You can also pass a variable (prop) into your template :
1109
1094
1110
1095
.. code-block :: html+twig
1111
1096
1112
- {# templates/components/Button.html.twig #}
1113
- {% props label, primary = true %}
1114
-
1115
- <button class="{{ primary ? 'primary' : 'secondary' }} large rounded">
1116
- {{ label }}
1117
- </button>
1118
-
1119
- And use it:
1120
-
1121
- .. code-block :: html+twig
1122
-
1123
- {# index.html.twig #}
1124
- ...
1097
+ {# index.html.twig #}
1098
+ ...
1125
1099
<div>
1126
- <twig:Button label=" Click Me!" / >
1100
+ <twig:Button icon="fa-plus" type="primary" role="button"> Click Me!</twig:Button >
1127
1101
</div>
1128
1102
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
1103
+ To tell the system that ``icon `` and ``type `` are props and not attributes, use the ``{% props %} `` tag at the top of your template.
1134
1104
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
1105
+ .. code-block :: html+twig
1145
1106
1146
- {# index.html.twig #}
1147
- ...
1148
- <div>
1149
- <twig:Button label="Click Me!" :disabled="true" />
1150
- </div>
1107
+ {# templates/components/Button.html.twig #}
1108
+ {% props icon = null, type = 'primary' %}
1151
1109
1152
- {# renders as: #}
1153
- <button class="disabled">Click Me!</button>
1110
+ <button {{ attributes.defaults({ class: 'btn btn-'~type }) }}>
1111
+ {% block content %}{% endblock %}
1112
+ {% if icon %}
1113
+ <span class="fa-solid fa-{{ icon }}"></span>
1114
+ {% endif %}
1115
+ </button>
1154
1116
1155
1117
Test Helpers
1156
1118
------------
0 commit comments