@@ -1050,6 +1050,70 @@ And in your component template you can access your embedded block
1050
1050
{% block footer %}{% endblock %}
1051
1051
</div>
1052
1052
1053
+ Anonymous Components
1054
+ --------------------
1055
+
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 `_):
1059
+
1060
+ .. code-block :: html+twig
1061
+
1062
+ {# templates/components/Button/Primary.html.twig #}
1063
+ <button {{ attributes.defaults({ class: 'primary' }) }}>
1064
+ {% block content %}{% endblock %}
1065
+ </button>
1066
+
1067
+ Then use your component with ``: `` to navigate through sub-directories (if there are any):
1068
+
1069
+ .. code-block :: html+twig
1070
+
1071
+ {# index.html.twig #}
1072
+ ...
1073
+ <div>
1074
+ <twig:Button: Primary>Click Me!</twig:Button: Primary>
1075
+ </div>
1076
+
1077
+ {# renders as: #}
1078
+ <button class="primary">Click Me!</button>
1079
+
1080
+ Like normal, you can pass extra attributes that will be rendered on the element:
1081
+
1082
+ .. code-block :: html+twig
1083
+
1084
+ {# index.html.twig #}
1085
+ ...
1086
+ <div>
1087
+ <twig:Button: Primary type="button" name="foo">Click Me!</twig:Button: Primary>
1088
+ </div>
1089
+
1090
+ {# renders as: #}
1091
+ <button class="primary" type="button" name="foo">Click Me!</button>
1092
+
1093
+ You can also pass a variable (prop) into your template:
1094
+
1095
+ .. code-block :: html+twig
1096
+
1097
+ {# index.html.twig #}
1098
+ ...
1099
+ <div>
1100
+ <twig:Button icon="fa-plus" type="primary" role="button">Click Me!</twig:Button>
1101
+ </div>
1102
+
1103
+ To tell the system that ``icon `` and ``type `` are props and not attributes, use the ``{% props %} `` tag at the top of your template.
1104
+
1105
+ .. code-block :: html+twig
1106
+
1107
+ {# templates/components/Button.html.twig #}
1108
+ {% props icon = null, type = 'primary' %}
1109
+
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>
1116
+
1053
1117
Test Helpers
1054
1118
------------
1055
1119
0 commit comments