Skip to content

Commit b192afc

Browse files
committed
remove init_live_component
1 parent 97e0c96 commit b192afc

File tree

5 files changed

+65
-28
lines changed

5 files changed

+65
-28
lines changed

src/LiveComponent/src/Resources/doc/index.rst

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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() }}>
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() }}>
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() }}>
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
239244
stateful because they will be autowired each time before the component
240245
is 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() }}>
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()>
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() }}
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()`` and then you render the form however you
819-
want.
861+
``attributes`` and then you render the form however you want.
820862

821863
But the result is incredible! As you finish changing each field, the
822864
component 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() }}
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()}}>
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() }}
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() }}
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() }}
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() }}>
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() }} 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

src/LiveComponent/src/Twig/LiveComponentExtension.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ final class LiveComponentExtension extends AbstractExtension
2424
public function getFunctions(): array
2525
{
2626
return [
27-
new TwigFunction('init_live_component', [LiveComponentRuntime::class, 'renderLiveAttributes'], ['needs_context' => true, 'is_safe' => ['html_attr']]),
2827
new TwigFunction('component_url', [LiveComponentRuntime::class, 'getComponentUrl']),
2928
];
3029
}

src/LiveComponent/src/Twig/LiveComponentRuntime.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,6 @@ public function __construct(
3434
) {
3535
}
3636

37-
public function renderLiveAttributes(array $context): string
38-
{
39-
if (!isset($context['_component_config'])) {
40-
throw new \LogicException('init_live_component can only be called within a component template.');
41-
}
42-
43-
return $this->getLiveAttributes($context['this'], $context['_component_config']);
44-
}
45-
4637
public function getComponentUrl(string $name, array $props = []): string
4738
{
4839
$component = $this->factory->create($name, $props);

src/TwigComponent/src/ComponentRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function render(object $component, array $config): string
4040
$event = new PreRenderEvent(
4141
$component,
4242
$config['template'],
43-
array_merge(['this' => $component, '_component_config' => $config], get_object_vars($component)),
43+
array_merge(['this' => $component], get_object_vars($component)),
4444
$config
4545
);
4646

src/TwigComponent/src/Resources/doc/index.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,10 @@ you can store its result on a private property:
418418
Component Attributes
419419
--------------------
420420

421+
.. versionadded:: 2.1
422+
423+
The ``HasAttributes`` trait was added in TwigComponents 2.1.
424+
421425
A common need for components is to configure/render attributes for the
422426
root node. You can enable this feature by having your component use
423427
the ``HasAttributesTrait``. Attributes are any data passed to ``component()``

0 commit comments

Comments
 (0)