Skip to content

Commit cc09617

Browse files
committed
minor #1058 [Docs] Anonymous twig components (Marie CHARLES)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Docs] Anonymous twig components | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Tickets | none | License | MIT Add documentation for the recently added support of anonymous twig components [https://github.com/symfony/ux/pull/802](https://github.com/symfony/ux/pull/802) Commits ------- 41f59f1 [Docs] Anonymous twig components
2 parents 079acf6 + 41f59f1 commit cc09617

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/TwigComponent/doc/index.rst

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,70 @@ And in your component template you can access your embedded block
10501050
{% block footer %}{% endblock %}
10511051
</div>
10521052

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+
10531117
Test Helpers
10541118
------------
10551119

0 commit comments

Comments
 (0)