@@ -1046,6 +1046,79 @@ And in your component template you can access your embedded block
10461046 {% block footer %}{% endblock %}
10471047 </div>
10481048
1049+ Anonymous TwigComponent
1050+ -----------------------
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 `_).
1056+
1057+ .. code-block :: html+twig
1058+
1059+ {# templates/components/Button/Primary.html.twig #}
1060+ <button class="primary">
1061+ {% block content %}
1062+ {% endblock %}
1063+ </button>
1064+
1065+ Then use your component with ``: `` to navigate through the directories:
1066+
1067+ .. code-block :: html+twig
1068+
1069+ {# index.html.twig #}
1070+ ...
1071+ <div>
1072+ <twig:Button: Primary>
1073+ Click Me!
1074+ </twig:primary>
1075+ </div>
1076+
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":
1081+
1082+ .. code-block :: html+twig
1083+
1084+ {# templates/components/Button.html.twig #}
1085+ {% props label, primary = true %}
1086+
1087+ <button class="{{ primary ? 'primary' : 'secondary' }} large rounded">
1088+ {{ label }}
1089+ </button>
1090+
1091+ Then use it:
1092+
1093+ .. code-block :: html+twig
1094+
1095+ {# index.html.twig #}
1096+ ...
1097+ <div>
1098+ <twig:Button label="Click Me!" />
1099+ </div>
1100+
1101+ You can also define your component to allow overwriting attributes:
1102+
1103+ .. code-block :: html+twig
1104+
1105+ {# templates/components/Button.html.twig #}
1106+ {% props label, primary = true %}
1107+
1108+ <button {{ attributes.defaults({class: primary ? 'primary' : 'secondary'}) }}>
1109+ {{ label }}
1110+ </button>
1111+
1112+ And use it like so:
1113+
1114+ .. code-block :: html+twig
1115+
1116+ {# index.html.twig #}
1117+ ...
1118+ <div>
1119+ <twig:Button label="Click Me!" :primary="false" />
1120+ </div>
1121+
10491122Test Helpers
10501123------------
10511124
0 commit comments