@@ -43,10 +43,15 @@ A real-time product search component might look like this::
4343 The ability to reference local variables in the template (e.g. ``query ``) was added in TwigComponents 2.1.
4444 Previously, all data needed to be referenced through ``this `` (e.g. ``this.query ``).
4545
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+
4651.. code-block :: twig
4752
4853 {# templates/components/product_search.html.twig #}
49- <div {{ init_live_component(this) }}>
54+ <div {{ attributes }}>
5055 <input
5156 type="search"
5257 name="query"
@@ -159,13 +164,13 @@ re-rendered live on the frontend), replace the component's
159164 }
160165
161166 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:
164169
165170.. code-block :: diff
166171
167172 - <div>
168- + <div {{ init_live_component(this) }}>
173+ + <div {{ attributes }}>
169174 <strong>{{ this.randomNumber }}</strong>
170175 </div>
171176
@@ -176,7 +181,7 @@ and give the user a new random number:
176181
177182.. code-block :: twig
178183
179- <div {{ init_live_component(this) }}>
184+ <div {{ attributes }}>
180185 <strong>{{ this.randomNumber }}</strong>
181186
182187 <button
@@ -239,6 +244,44 @@ exceptions being properties that hold services (these don't need to be
239244stateful because they will be autowired each time before the component
240245is rendered) and `properties used for computed properties `_.
241246
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+
242285 data-action=“live#update”: Re-rendering on LiveProp Change
243286----------------------------------------------------------
244287
@@ -251,7 +294,7 @@ Let's add two inputs to our template:
251294.. code-block :: twig
252295
253296 {# templates/components/random_number.html.twig #}
254- <div {{ init_live_component(this) }}>
297+ <div {{ attributes }}>
255298 <input
256299 type="number"
257300 value="{{ min }}"
@@ -368,7 +411,7 @@ property. The following code works identically to the previous example:
368411
369412.. code-block :: diff
370413
371- <div {{ init_live_component(this) >
414+ <div {{ attributes }} >
372415 <input
373416 type="number"
374417 value="{{ min }}"
@@ -791,7 +834,7 @@ as ``this.form`` thanks to the trait:
791834
792835 {# templates/components/post_form.html.twig #}
793836 <div
794- {{ init_live_component(this) }}
837+ {{ attributes }}
795838 {#
796839 Automatically catch all "change" events from the fields
797840 below and re-render the component.
@@ -815,8 +858,7 @@ as ``this.form`` thanks to the trait:
815858 </div>
816859
817860 Mostly, this is a pretty boring template! It includes the normal
818- ``init_live_component(this) `` and then you render the form however you
819- want.
861+ ``attributes `` and then you render the form however you want.
820862
821863But the result is incredible! As you finish changing each field, the
822864component automatically re-renders - including showing any validation
@@ -1024,7 +1066,7 @@ section above) is to add:
10241066.. code-block :: diff
10251067
10261068 <div
1027- {{ init_live_component(this) }}
1069+ {{ attributes }}
10281070 + data-action="change->live#update"
10291071 >
10301072
@@ -1056,7 +1098,7 @@ rendered the ``content`` through a Markdown filter from the
10561098
10571099.. code-block :: twig
10581100
1059- <div {{init_live_component(this) }}>
1101+ <div {{ attributes }}>
10601102 <input
10611103 type="text"
10621104 value="{{ post.title }}"
@@ -1221,7 +1263,7 @@ You can also use “polling” to continually refresh a component. On the
12211263.. code-block :: diff
12221264
12231265 <div
1224- {{ init_live_component(this) }}
1266+ {{ attributes }}
12251267 + data-poll
12261268 >
12271269
@@ -1233,7 +1275,7 @@ delay for 500ms:
12331275.. code-block :: twig
12341276
12351277 <div
1236- {{ init_live_component(this) }}
1278+ {{ attributes }}
12371279 data-poll="delay(500)|$render"
12381280 >
12391281
@@ -1242,7 +1284,7 @@ You can also trigger a specific “action” instead of a normal re-render:
12421284.. code-block :: twig
12431285
12441286 <div
1245- {{ init_live_component(this) }}
1287+ {{ attributes }}
12461288
12471289 data-poll="save"
12481290 {#
@@ -1437,7 +1479,7 @@ In the ``EditPostComponent`` template, you render the
14371479.. code-block :: twig
14381480
14391481 {# templates/components/edit_post.html.twig #}
1440- <div {{ init_live_component(this) }}>
1482+ <div {{ attributes }}>
14411483 <input
14421484 type="text"
14431485 name="post[title]"
@@ -1459,7 +1501,7 @@ In the ``EditPostComponent`` template, you render the
14591501
14601502 .. code-block :: twig
14611503
1462- <div {{ init_live_component(this) }} class="mb-3">
1504+ <div {{ attributes }} class="mb-3">
14631505 <textarea
14641506 name="{{ name }}"
14651507 data-model="value"
@@ -1496,3 +1538,4 @@ bound to Symfony's BC policy for the moment.
14961538.. _`experimental` : https://symfony.com/doc/current/contributing/code/experimental.html
14971539.. _`dependent form fields` : https://symfony.com/doc/current/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms
14981540.. _`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