Skip to content

Commit 255def9

Browse files
author
Marie CHARLES
committed
add updates after #1041 fix
1 parent 780100a commit 255def9

File tree

1 file changed

+49
-20
lines changed

1 file changed

+49
-20
lines changed

src/TwigComponent/doc/index.rst

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,10 +1049,9 @@ And in your component template you can access your embedded block
10491049
Anonymous TwigComponent
10501050
-----------------------
10511051

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`_):
10561055

10571056
.. code-block:: html+twig
10581057

@@ -1062,7 +1061,7 @@ No need for class. Your component matches the namespace of the file you created
10621061
{% endblock %}
10631062
</button>
10641063

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):
10661065

10671066
.. code-block:: html+twig
10681067

@@ -1071,54 +1070,84 @@ Then use your component with ``:`` to navigate through the directories:
10711070
<div>
10721071
<twig:Button:Primary>
10731072
Click Me!
1074-
</twig:primary>
1073+
</twig:Button:Primary>
10751074
</div>
10761075

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:
10811077

10821078
.. code-block:: html+twig
10831079

10841080
{# 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 %}
10891084
</button>
10901085

1091-
Then use it:
1086+
Then pass any further data to your reusable "button" like so:
10921087

10931088
.. code-block:: html+twig
10941089

10951090
{# index.html.twig #}
10961091
...
10971092
<div>
1098-
<twig:Button label="Click Me!" />
1093+
<twig:Button role="button" class="foo">
1094+
Click Me!
1095+
</twig:Button>
10991096
</div>
11001097

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":
11021105

11031106
.. code-block:: html+twig
11041107

11051108
{# templates/components/Button.html.twig #}
11061109
{% props label, primary = true %}
11071110

1108-
<button {{ attributes.defaults({class: primary ? 'primary' : 'secondary'}) }}>
1111+
<button class="{{ primary ? 'primary' : 'secondary' }} large rounded">
11091112
{{ label }}
11101113
</button>
11111114

1112-
And use it like so:
1115+
And use it:
11131116

11141117
.. code-block:: html+twig
11151118

11161119
{# index.html.twig #}
11171120
...
11181121
<div>
1119-
<twig:Button label="Click Me!" :primary="false" />
1122+
<twig:Button label="Click Me!" />
11201123
</div>
11211124

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+
11221151
Test Helpers
11231152
------------
11241153

0 commit comments

Comments
 (0)