Skip to content

Commit d9d4dc6

Browse files
author
Marie CHARLES
committed
add updates after #1041 fix
1 parent e5bda17 commit d9d4dc6

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
@@ -1053,10 +1053,9 @@ And in your component template you can access your embedded block
10531053
Anonymous TwigComponent
10541054
-----------------------
10551055

1056-
Sometimes, reusable elements do not require complex logic or injected service to render what it could be processed
1057-
from one template itself. (e.g. retrieving a customized primary button that could take a different label).
1058-
1059-
No need for class. Your component matches the namespace of the file you created where components live (see `Twig Template Namespaces`_).
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`_):
10601059

10611060
.. code-block:: html+twig
10621061

@@ -1066,7 +1065,7 @@ No need for class. Your component matches the namespace of the file you created
10661065
{% endblock %}
10671066
</button>
10681067

1069-
Then use your component with ``:`` to navigate through the directories:
1068+
Then use your component with ``:`` to navigate through sub-directories (if there are any):
10701069

10711070
.. code-block:: html+twig
10721071

@@ -1075,54 +1074,84 @@ Then use your component with ``:`` to navigate through the directories:
10751074
<div>
10761075
<twig:Button:Primary>
10771076
Click Me!
1078-
</twig:primary>
1077+
</twig:Button:Primary>
10791078
</div>
10801079

1081-
Now let's pass props to your button by defining a ``{% props %}`` tag in its view.
1082-
It allows to declare which props are required and which have a default value.
1083-
1084-
For a required prop "label", and one optional prop "primary":
1080+
Still get all your attributes by rendering the ``attributes`` object in your template:
10851081

10861082
.. code-block:: html+twig
10871083

10881084
{# templates/components/Button.html.twig #}
1089-
{% props label, primary = true %}
1090-
1091-
<button class="{{ primary ? 'primary' : 'secondary' }} large rounded">
1092-
{{ label }}
1085+
<button {{ attributes }}>
1086+
{% block content %}
1087+
{% endblock %}
10931088
</button>
10941089

1095-
Then use it:
1090+
Then pass any further data to your reusable "button" like so:
10961091

10971092
.. code-block:: html+twig
10981093

10991094
{# index.html.twig #}
11001095
...
11011096
<div>
1102-
<twig:Button label="Click Me!" />
1097+
<twig:Button role="button" class="foo">
1098+
Click Me!
1099+
</twig:Button>
11031100
</div>
11041101

1105-
You can also define your component to allow overwriting attributes:
1102+
{# 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.
1107+
1108+
For a required prop "label", and one optional prop "primary":
11061109

11071110
.. code-block:: html+twig
11081111

11091112
{# templates/components/Button.html.twig #}
11101113
{% props label, primary = true %}
11111114

1112-
<button {{ attributes.defaults({class: primary ? 'primary' : 'secondary'}) }}>
1115+
<button class="{{ primary ? 'primary' : 'secondary' }} large rounded">
11131116
{{ label }}
11141117
</button>
11151118

1116-
And use it like so:
1119+
And use it:
11171120

11181121
.. code-block:: html+twig
11191122

11201123
{# index.html.twig #}
11211124
...
11221125
<div>
1123-
<twig:Button label="Click Me!" :primary="false" />
1126+
<twig:Button label="Click Me!" />
11241127
</div>
11251128

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
1134+
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
1145+
1146+
{# index.html.twig #}
1147+
...
1148+
<div>
1149+
<twig:Button label="Click Me!" :disabled="true" />
1150+
</div>
1151+
1152+
{# renders as: #}
1153+
<button class="disabled">Click Me!</button>
1154+
11261155
Test Helpers
11271156
------------
11281157

0 commit comments

Comments
 (0)