Skip to content

Commit 2e1ea5a

Browse files
author
Marie CHARLES
committed
add improvements to examples
1 parent d9d4dc6 commit 2e1ea5a

File tree

1 file changed

+28
-66
lines changed

1 file changed

+28
-66
lines changed

src/TwigComponent/doc/index.rst

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

1053-
Anonymous TwigComponent
1054-
-----------------------
1053+
Anonymous Components
1054+
--------------------
10551055

10561056
Sometimes a component is simple enough that it doesn't have any complex logic or injected services.
10571057
In this case, you can skip the class and only create the template. The component name is determined
@@ -1060,97 +1060,59 @@ by the location of the template (see `Twig Template Namespaces`_):
10601060
.. code-block:: html+twig
10611061

10621062
{# templates/components/Button/Primary.html.twig #}
1063-
<button class="primary">
1064-
{% block content %}
1065-
{% endblock %}
1063+
<button {{ attributes.defaults({ class: 'primary' }) }}>
1064+
{% block content %}{% endblock %}
10661065
</button>
10671066

10681067
Then use your component with ``:`` to navigate through sub-directories (if there are any):
10691068

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

1072-
{# index.html.twig #}
1073-
...
1071+
{# index.html.twig #}
1072+
...
10741073
<div>
1075-
<twig:Button:Primary>
1076-
Click Me!
1077-
</twig:Button:Primary>
1074+
<twig:Button:Primary>Click Me!</twig:Button:Primary>
10781075
</div>
10791076

1080-
Still get all your attributes by rendering the ``attributes`` object in your template:
1081-
1082-
.. code-block:: html+twig
1083-
1084-
{# templates/components/Button.html.twig #}
1085-
<button {{ attributes }}>
1086-
{% block content %}
1087-
{% endblock %}
1088-
</button>
1077+
{# renders as: #}
1078+
<button class="primary">Click Me!</button>
10891079

1090-
Then pass any further data to your reusable "button" like so:
1080+
Like normal, you can pass extra attributes that will be rendered on the element:
10911081

10921082
.. code-block:: html+twig
10931083

1094-
{# index.html.twig #}
1095-
...
1084+
{# index.html.twig #}
1085+
...
10961086
<div>
1097-
<twig:Button role="button" class="foo">
1098-
Click Me!
1099-
</twig:Button>
1087+
<twig:Button:Primary type="button" name="foo">Click Me!</twig:Button:Primary>
11001088
</div>
11011089

11021090
{# 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.
1091+
<button class="primary" type="button" name="foo">Click Me!</button>
11071092

1108-
For a required prop "label", and one optional prop "primary":
1093+
You can also pass a variable (prop) into your template:
11091094

11101095
.. code-block:: html+twig
11111096

1112-
{# templates/components/Button.html.twig #}
1113-
{% props label, primary = true %}
1114-
1115-
<button class="{{ primary ? 'primary' : 'secondary' }} large rounded">
1116-
{{ label }}
1117-
</button>
1118-
1119-
And use it:
1120-
1121-
.. code-block:: html+twig
1122-
1123-
{# index.html.twig #}
1124-
...
1097+
{# index.html.twig #}
1098+
...
11251099
<div>
1126-
<twig:Button label="Click Me!" />
1100+
<twig:Button icon="fa-plus" type="primary" role="button">Click Me!</twig:Button>
11271101
</div>
11281102

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
1103+
To tell the system that ``icon`` and ``type`` are props and not attributes, use the ``{% props %}`` tag at the top of your template.
11341104

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
1105+
.. code-block:: html+twig
11451106

1146-
{# index.html.twig #}
1147-
...
1148-
<div>
1149-
<twig:Button label="Click Me!" :disabled="true" />
1150-
</div>
1107+
{# templates/components/Button.html.twig #}
1108+
{% props icon = null, type = 'primary' %}
11511109

1152-
{# renders as: #}
1153-
<button class="disabled">Click Me!</button>
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>
11541116

11551117
Test Helpers
11561118
------------

0 commit comments

Comments
 (0)