Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Turbo] Use blocks instead of partials to render turbo-streams #1045

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions src/Turbo/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ Let's discover how to use Turbo Streams to enhance your `Symfony forms`_::
if (TurboBundle::STREAM_FORMAT === $request->getPreferredFormat()) {
// If the request comes from Turbo, set the content type as text/vnd.turbo-stream.html and only send the HTML to update
$request->setRequestFormat(TurboBundle::STREAM_FORMAT);
return $this->render('task/success.stream.html.twig', ['task' => $task]);
return $this->renderBlock('task/new.html.twig', 'success_stream', ['task' => $task]);
}

// If the client doesn't support JavaScript, or isn't using Turbo, the form still works as usual.
Expand All @@ -362,14 +362,16 @@ Let's discover how to use Turbo Streams to enhance your `Symfony forms`_::

.. code-block:: html+twig

{# success.stream.html.twig #}
<turbo-stream action="replace" target="my_div_id">
{# bottom of new.html.twig #}
{% block success_stream %}
<turbo-stream action="replace" targets="#my_div_id">
<template>
The element having the id "my_div_id" will be replaced by this block, without a full page reload!

<div>The task "{{ task }}" has been created!</div>
</template>
</turbo-stream>
{% endblock %}

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

To do that, first isolate your form rendering into a template partial so you can
reuse it. Also surround the form by an element with an ``id`` so you can target
it from the stream:
To do that, first isolate your form rendering into a block so you can reuse it:

.. code-block:: html+twig
.. code-block:: diff

{# templates/task/_form.html.twig #}
<div id="task-form">
{# render your form however you want #}
{{ form(form) }}
</div>
{# new.html.twig #}
+{% block task_form %}
{{ form(form) }}
+{% endblock %}

Include this from your existing template (e.g. `new.html.twig`) to render it.
Now, create a "fresh" form and pass it into your stream:

.. code-block:: diff
Expand All @@ -408,7 +406,7 @@ Now, create a "fresh" form and pass it into your stream:
{
$form = $this->createForm(TaskType::class, new Task());

+ $emptyForm = clone $form ;
+ $emptyForm = clone $form;
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
Expand All @@ -417,7 +415,7 @@ Now, create a "fresh" form and pass it into your stream:
if (TurboBundle::STREAM_FORMAT === $request->getPreferredFormat()) {
$request->setRequestFormat(TurboBundle::STREAM_FORMAT);

return $this->render('task/success.stream.html.twig', [
return $this->renderBlock('task/new.html.twig', 'success_stream', [
'task' => $task,
+ 'form' => $emptyForm,
]);
Expand All @@ -435,14 +433,16 @@ Now, create a "fresh" form and pass it into your stream:

Now, in your stream template, "replace" the entire form:

.. code-block:: html+twig
.. code-block:: diff

{# success.stream.html.twig #}
<turbo-stream action="replace" target="task-form">
<template>
{{ include('task/_form.html.twig') }}
</template>
</turbo-stream>
{# new.html.twig #}
{% block success_stream %}
+<turbo-stream action="replace" targets="form[name=task]">
+ <template>
+ {{ block('task_form') }}
+ </template>
+</turbo-stream>
<turbo-stream action="replace" targets="#my_div_id">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing the closing block and this last turbo-stream looks extra to me

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well actually, this is a diff that applies to the snippet on L366, so yes, the endblock is missing and this line is here because this is just a subset of the full file


.. _chat-example:

Expand Down Expand Up @@ -567,7 +567,7 @@ Let's create our chat::

{# chat/message.stream.html.twig #}
{# New messages received through the Mercure connection are appended to the div with the "messages" ID. #}
<turbo-stream action="append" target="messages">
<turbo-stream action="append" targets="#messages">
<template>
<div>{{ message }}</div>
</template>
Expand Down Expand Up @@ -624,23 +624,23 @@ created, modified or deleted:

{# templates/broadcast/Book.stream.html.twig #}
{% block create %}
<turbo-stream action="append" target="books">
<turbo-stream action="append" targets="#books">
<template>
<div id="{{ 'book_' ~ id }}">{{ entity.title }} (#{{ id }})</div>
</template>
</turbo-stream>
{% endblock %}

{% block update %}
<turbo-stream action="update" target="book_{{ id }}">
<turbo-stream action="update" targets="#book_{{ id }}">
<template>
{{ entity.title }} (#{{ id }}, updated)
</template>
</turbo-stream>
{% endblock %}

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

By convention, Symfony UX Turbo will look for a template named
Expand Down
Loading