Skip to content

Commit e8a68ad

Browse files
[Turbo] Use blocks instead of partials to render turbo-streams
1 parent 7c2861d commit e8a68ad

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

src/Turbo/doc/index.rst

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ Let's discover how to use Turbo Streams to enhance your `Symfony forms`_::
345345
if (TurboBundle::STREAM_FORMAT === $request->getPreferredFormat()) {
346346
// If the request comes from Turbo, set the content type as text/vnd.turbo-stream.html and only send the HTML to update
347347
$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]);
349349
}
350350

351351
// 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`_::
362362

363363
.. code-block:: html+twig
364364

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">
367368
<template>
368369
The element having the id "my_div_id" will be replaced by this block, without a full page reload!
369370

370371
<div>The task "{{ task }}" has been created!</div>
371372
</template>
372373
</turbo-stream>
374+
{% endblock %}
373375

374376
Supported actions are ``append``, ``prepend``, ``replace``, ``update``,
375377
``remove``, ``before`` and ``after``.
@@ -382,19 +384,15 @@ When you return a Turbo stream, *only* the elements in that stream template will
382384
be updated. This means that if you want to reset the form, you need to include
383385
a new form in the stream template.
384386

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:
388388

389-
.. code-block:: html+twig
389+
.. code-block:: diff
390390
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 %}
396395
397-
Include this from your existing template (e.g. `new.html.twig`) to render it.
398396
Now, create a "fresh" form and pass it into your stream:
399397

400398
.. code-block:: diff
@@ -408,7 +406,7 @@ Now, create a "fresh" form and pass it into your stream:
408406
{
409407
$form = $this->createForm(TaskType::class, new Task());
410408
411-
+ $emptyForm = clone $form ;
409+
+ $emptyForm = clone $form;
412410
$form->handleRequest($request);
413411
414412
if ($form->isSubmitted() && $form->isValid()) {
@@ -417,7 +415,7 @@ Now, create a "fresh" form and pass it into your stream:
417415
if (TurboBundle::STREAM_FORMAT === $request->getPreferredFormat()) {
418416
$request->setRequestFormat(TurboBundle::STREAM_FORMAT);
419417
420-
return $this->render('task/success.stream.html.twig', [
418+
return $this->renderBlock('task/new.html.twig', 'success_stream', [
421419
'task' => $task,
422420
+ 'form' => $emptyForm,
423421
]);
@@ -435,14 +433,16 @@ Now, create a "fresh" form and pass it into your stream:
435433
436434
Now, in your stream template, "replace" the entire form:
437435

438-
.. code-block:: html+twig
436+
.. code-block:: diff
439437
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">
446446
447447
.. _chat-example:
448448

@@ -567,7 +567,7 @@ Let's create our chat::
567567

568568
{# chat/message.stream.html.twig #}
569569
{# 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">
571571
<template>
572572
<div>{{ message }}</div>
573573
</template>
@@ -624,23 +624,23 @@ created, modified or deleted:
624624

625625
{# templates/broadcast/Book.stream.html.twig #}
626626
{% block create %}
627-
<turbo-stream action="append" target="books">
627+
<turbo-stream action="append" targets="#books">
628628
<template>
629629
<div id="{{ 'book_' ~ id }}">{{ entity.title }} (#{{ id }})</div>
630630
</template>
631631
</turbo-stream>
632632
{% endblock %}
633633

634634
{% block update %}
635-
<turbo-stream action="update" target="book_{{ id }}">
635+
<turbo-stream action="update" targets="#book_{{ id }}">
636636
<template>
637637
{{ entity.title }} (#{{ id }}, updated)
638638
</template>
639639
</turbo-stream>
640640
{% endblock %}
641641

642642
{% block remove %}
643-
<turbo-stream action="remove" target="book_{{ id }}"></turbo-stream>
643+
<turbo-stream action="remove" targets="#book_{{ id }}"></turbo-stream>
644644
{% endblock %}
645645

646646
By convention, Symfony UX Turbo will look for a template named

0 commit comments

Comments
 (0)