@@ -43,10 +43,15 @@ A real-time product search component might look like this::
43
43
The ability to reference local variables in the template (e.g. ``query ``) was added in TwigComponents 2.1.
44
44
Previously, all data needed to be referenced through ``this `` (e.g. ``this.query ``).
45
45
46
+ .. versionadded :: 2.1
47
+
48
+ The ability to initialize live component with the ``attributes `` twig variable was added in LiveComponents
49
+ 2.1. Previously, the ``init_live_component() `` function was required (this function was removed in 2.1).
50
+
46
51
.. code-block :: twig
47
52
48
53
{# templates/components/product_search.html.twig #}
49
- <div {{ init_live_component() }}>
54
+ <div {{ attributes }}>
50
55
<input
51
56
type="search"
52
57
name="query"
@@ -159,13 +164,13 @@ re-rendered live on the frontend), replace the component's
159
164
}
160
165
161
166
Then, in the template, make sure there is *one * HTML element around your
162
- entire component and use the ``{{ init_live_component() }} `` function to
163
- initialize the Stimulus controller:
167
+ entire component and use the ``{{ attributes }} `` variable to initialize
168
+ the Stimulus controller:
164
169
165
170
.. code-block :: diff
166
171
167
172
- <div>
168
- + <div {{ init_live_component() }}>
173
+ + <div {{ attributes }}>
169
174
<strong>{{ this.randomNumber }}</strong>
170
175
</div>
171
176
@@ -176,7 +181,7 @@ and give the user a new random number:
176
181
177
182
.. code-block :: twig
178
183
179
- <div {{ init_live_component() }}>
184
+ <div {{ attributes }}>
180
185
<strong>{{ this.randomNumber }}</strong>
181
186
182
187
<button
@@ -239,6 +244,44 @@ exceptions being properties that hold services (these don't need to be
239
244
stateful because they will be autowired each time before the component
240
245
is rendered) and `properties used for computed properties `_.
241
246
247
+ Component Attributes
248
+ --------------------
249
+
250
+ .. versionadded :: 2.1
251
+
252
+ The ``HasAttributes `` trait was added in TwigComponents 2.1.
253
+
254
+ `Component attributes `_ allows you to render your components with extra
255
+ props that are are converted to html attributes and made available in
256
+ your component's template as an ``attributes `` variable. When used on
257
+ live components, these props are persisted between renders. You can enable
258
+ this feature by having your live component use the ``HasAttributesTrait ``:
259
+
260
+ .. code-block :: diff
261
+
262
+ // ...
263
+ use Symfony\UX\LiveComponent\Attribute\LiveProp;
264
+ + use Symfony\UX\TwigComponent\HasAttributesTrait;
265
+
266
+ #[AsLiveComponent('random_number')]
267
+ class RandomNumberComponent
268
+ {
269
+ + use HasAttributesTrait;
270
+
271
+ #[LiveProp]
272
+ public int $min = 0;
273
+
274
+ Now, when rendering your component, you can pass html attributes
275
+ as props and these will be added to ``attributes ``:
276
+
277
+ .. code-block :: twig
278
+
279
+ {{ component('random_number', { min: 5, max: 500, class: 'widget', style: 'color: black;' }) }}
280
+
281
+ {# renders as: #}
282
+ <div class="widget" style="color: black;" <!-- other live attributes -->>
283
+ <!-- ... -->
284
+
242
285
data-action=“live#update”: Re-rendering on LiveProp Change
243
286
----------------------------------------------------------
244
287
@@ -251,7 +294,7 @@ Let's add two inputs to our template:
251
294
.. code-block :: twig
252
295
253
296
{# templates/components/random_number.html.twig #}
254
- <div {{ init_live_component() }}>
297
+ <div {{ attributes }}>
255
298
<input
256
299
type="number"
257
300
value="{{ min }}"
@@ -368,7 +411,7 @@ property. The following code works identically to the previous example:
368
411
369
412
.. code-block :: diff
370
413
371
- <div {{ init_live_component() >
414
+ <div {{ attributes }} >
372
415
<input
373
416
type="number"
374
417
value="{{ min }}"
@@ -791,7 +834,7 @@ as ``this.form`` thanks to the trait:
791
834
792
835
{# templates/components/post_form.html.twig #}
793
836
<div
794
- {{ init_live_component() }}
837
+ {{ attributes }}
795
838
{#
796
839
Automatically catch all "change" events from the fields
797
840
below and re-render the component.
@@ -815,8 +858,7 @@ as ``this.form`` thanks to the trait:
815
858
</div>
816
859
817
860
Mostly, this is a pretty boring template! It includes the normal
818
- ``init_live_component() `` and then you render the form however you
819
- want.
861
+ ``attributes `` and then you render the form however you want.
820
862
821
863
But the result is incredible! As you finish changing each field, the
822
864
component automatically re-renders - including showing any validation
@@ -1024,7 +1066,7 @@ section above) is to add:
1024
1066
.. code-block :: diff
1025
1067
1026
1068
<div
1027
- {{ init_live_component() }}
1069
+ {{ attributes }}
1028
1070
+ data-action="change->live#update"
1029
1071
>
1030
1072
@@ -1056,7 +1098,7 @@ rendered the ``content`` through a Markdown filter from the
1056
1098
1057
1099
.. code-block :: twig
1058
1100
1059
- <div {{init_live_component() }}>
1101
+ <div {{ attributes }}>
1060
1102
<input
1061
1103
type="text"
1062
1104
value="{{ post.title }}"
@@ -1221,7 +1263,7 @@ You can also use “polling” to continually refresh a component. On the
1221
1263
.. code-block :: diff
1222
1264
1223
1265
<div
1224
- {{ init_live_component() }}
1266
+ {{ attributes }}
1225
1267
+ data-poll
1226
1268
>
1227
1269
@@ -1233,7 +1275,7 @@ delay for 500ms:
1233
1275
.. code-block :: twig
1234
1276
1235
1277
<div
1236
- {{ init_live_component() }}
1278
+ {{ attributes }}
1237
1279
data-poll="delay(500)|$render"
1238
1280
>
1239
1281
@@ -1242,7 +1284,7 @@ You can also trigger a specific “action” instead of a normal re-render:
1242
1284
.. code-block :: twig
1243
1285
1244
1286
<div
1245
- {{ init_live_component() }}
1287
+ {{ attributes }}
1246
1288
1247
1289
data-poll="save"
1248
1290
{#
@@ -1437,7 +1479,7 @@ In the ``EditPostComponent`` template, you render the
1437
1479
.. code-block :: twig
1438
1480
1439
1481
{# templates/components/edit_post.html.twig #}
1440
- <div {{ init_live_component() }}>
1482
+ <div {{ attributes }}>
1441
1483
<input
1442
1484
type="text"
1443
1485
name="post[title]"
@@ -1459,7 +1501,7 @@ In the ``EditPostComponent`` template, you render the
1459
1501
1460
1502
.. code-block :: twig
1461
1503
1462
- <div {{ init_live_component() }} class="mb-3">
1504
+ <div {{ attributes }} class="mb-3">
1463
1505
<textarea
1464
1506
name="{{ name }}"
1465
1507
data-model="value"
@@ -1496,3 +1538,4 @@ bound to Symfony's BC policy for the moment.
1496
1538
.. _`experimental` : https://symfony.com/doc/current/contributing/code/experimental.html
1497
1539
.. _`dependent form fields` : https://symfony.com/doc/current/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms
1498
1540
.. _`Symfony UX configured in your app` : https://symfony.com/doc/current/frontend/ux.html
1541
+ .. _`Component attributes` : https://symfony.com/bundles/ux-twig-component/current/index.html#component-attributes
0 commit comments