@@ -345,7 +345,7 @@ Let's discover how to use Turbo Streams to enhance your `Symfony forms`_::
345
345
if (TurboBundle::STREAM_FORMAT === $request->getPreferredFormat()) {
346
346
// If the request comes from Turbo, set the content type as text/vnd.turbo-stream.html and only send the HTML to update
347
347
$request->setRequestFormat(TurboBundle::STREAM_FORMAT);
348
- return $this->render ('task/success.stream. html.twig', ['task' => $task]);
348
+ return $this->renderBlock ('task/new. html.twig', 'success_stream ', ['task' => $task]);
349
349
}
350
350
351
351
// If the client doesn't support JavaScript, or isn't using Turbo, the form still works as usual.
@@ -362,14 +362,16 @@ Let's discover how to use Turbo Streams to enhance your `Symfony forms`_::
362
362
363
363
.. code-block :: html+twig
364
364
365
- {# success.stream.html.twig #}
366
- <turbo-stream action="replace" target="my_div_id">
365
+ {# bottom of new.html.twig #}
366
+ {% block success_stream %}
367
+ <turbo-stream action="replace" targets="#my_div_id">
367
368
<template>
368
369
The element having the id "my_div_id" will be replaced by this block, without a full page reload!
369
370
370
371
<div>The task "{{ task }}" has been created!</div>
371
372
</template>
372
373
</turbo-stream>
374
+ {% endblock %}
373
375
374
376
Supported actions are ``append ``, ``prepend ``, ``replace ``, ``update ``,
375
377
``remove ``, ``before `` and ``after ``.
@@ -382,19 +384,15 @@ When you return a Turbo stream, *only* the elements in that stream template will
382
384
be updated. This means that if you want to reset the form, you need to include
383
385
a new form in the stream template.
384
386
385
- To do that, first isolate your form rendering into a template partial so you can
386
- reuse it. Also surround the form by an element with an ``id `` so you can target
387
- it from the stream:
387
+ To do that, first isolate your form rendering into a block so you can reuse it:
388
388
389
- .. code-block :: html+twig
389
+ .. code-block :: diff
390
390
391
- {# templates/task/_form.html.twig #}
392
- <div id="task-form">
393
- {# render your form however you want #}
394
- {{ form(form) }}
395
- </div>
391
+ {# new.html.twig #}
392
+ +{% block task_form %}
393
+ {{ form(form) }}
394
+ +{% endblock %}
396
395
397
- Include this from your existing template (e.g. `new.html.twig `) to render it.
398
396
Now, create a "fresh" form and pass it into your stream:
399
397
400
398
.. code-block :: diff
@@ -408,7 +406,7 @@ Now, create a "fresh" form and pass it into your stream:
408
406
{
409
407
$form = $this->createForm(TaskType::class, new Task());
410
408
411
- + $emptyForm = clone $form ;
409
+ + $emptyForm = clone $form;
412
410
$form->handleRequest($request);
413
411
414
412
if ($form->isSubmitted() && $form->isValid()) {
@@ -417,7 +415,7 @@ Now, create a "fresh" form and pass it into your stream:
417
415
if (TurboBundle::STREAM_FORMAT === $request->getPreferredFormat()) {
418
416
$request->setRequestFormat(TurboBundle::STREAM_FORMAT);
419
417
420
- return $this->render ('task/success.stream. html.twig', [
418
+ return $this->renderBlock ('task/new. html.twig', 'success_stream ', [
421
419
'task' => $task,
422
420
+ 'form' => $emptyForm,
423
421
]);
@@ -435,14 +433,16 @@ Now, create a "fresh" form and pass it into your stream:
435
433
436
434
Now, in your stream template, "replace" the entire form:
437
435
438
- .. code-block :: html+twig
436
+ .. code-block :: diff
439
437
440
- {# success.stream.html.twig #}
441
- <turbo-stream action="replace" target="task-form">
442
- <template>
443
- {{ include('task/_form.html.twig') }}
444
- </template>
445
- </turbo-stream>
438
+ {# new.html.twig #}
439
+ {% block success_stream %}
440
+ +<turbo-stream action="replace" targets="form[name=task]">
441
+ + <template>
442
+ + {{ block('task_form') }}
443
+ + </template>
444
+ +</turbo-stream>
445
+ <turbo-stream action="replace" targets="#my_div_id">
446
446
447
447
.. _chat-example :
448
448
@@ -567,7 +567,7 @@ Let's create our chat::
567
567
568
568
{# chat/message.stream.html.twig #}
569
569
{# New messages received through the Mercure connection are appended to the div with the "messages" ID. #}
570
- <turbo-stream action="append" target=" messages">
570
+ <turbo-stream action="append" targets="# messages">
571
571
<template>
572
572
<div>{{ message }}</div>
573
573
</template>
@@ -624,23 +624,23 @@ created, modified or deleted:
624
624
625
625
{# templates/broadcast/Book.stream.html.twig #}
626
626
{% block create %}
627
- <turbo-stream action="append" target=" books">
627
+ <turbo-stream action="append" targets="# books">
628
628
<template>
629
629
<div id="{{ 'book _' ~ id }}">{{ entity.title }} (#{{ id }})</div>
630
630
</template>
631
631
</turbo-stream>
632
632
{% endblock %}
633
633
634
634
{% block update %}
635
- <turbo-stream action="update" target=" book_{{ id }}">
635
+ <turbo-stream action="update" targets="# book_{{ id }}">
636
636
<template>
637
637
{{ entity.title }} (#{{ id }}, updated)
638
638
</template>
639
639
</turbo-stream>
640
640
{% endblock %}
641
641
642
642
{% block remove %}
643
- <turbo-stream action="remove" target=" book_{{ id }}"></turbo-stream>
643
+ <turbo-stream action="remove" targets="# book_{{ id }}"></turbo-stream>
644
644
{% endblock %}
645
645
646
646
By convention, Symfony UX Turbo will look for a template named
0 commit comments