Skip to content

Add <twig:Turbo:Stream:*> components #2227

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

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
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
14 changes: 11 additions & 3 deletions .github/workflows/test-turbo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ on:
paths:
- 'src/Turbo/**'

env:
SYMFONY_REQUIRE: '>=5.4'

jobs:
phpstan:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -38,11 +41,11 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: ['8.1', '8.3']
php-version: ['8.1', '8.4']
include:
- php-version: '8.1'
dependency-version: 'lowest'
- php-version: '8.3'
- php-version: '8.4'
dependency-version: 'highest'

services:
Expand All @@ -69,6 +72,9 @@ jobs:
with:
php-version: ${{ matrix.php-version }}

- name: Install symfony/flex
run: composer global config allow-plugins.symfony/flex true && composer global require symfony/flex

- name: Install Turbo packages
uses: ramsey/composer-install@v3
with:
Expand Down Expand Up @@ -101,6 +107,8 @@ jobs:

- name: Run tests
working-directory: src/Turbo
run: vendor/bin/simple-phpunit
run: |
[ 'lowest' = '${{ matrix.dependency-version }}' ] && export SYMFONY_DEPRECATIONS_HELPER=weak
vendor/bin/simple-phpunit
env:
SYMFONY_DEPRECATIONS_HELPER: 'max[self]=1'
1 change: 1 addition & 0 deletions src/Turbo/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Add `Helper/TurboStream::append()` et al. methods
- Add `TurboStreamResponse`
- Add `<twig:Turbo:Stream:*>` components

## 2.19.0

Expand Down
1 change: 1 addition & 0 deletions src/Turbo/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"symfony/property-access": "^5.4|^6.0|^7.0",
"symfony/security-core": "^5.4|^6.0|^7.0",
"symfony/stopwatch": "^5.4|^6.0|^7.0",
"symfony/ux-twig-component": "^2.21",
"symfony/twig-bundle": "^5.4|^6.0|^7.0",
"symfony/web-profiler-bundle": "^5.4|^6.0|^7.0",
"symfony/webpack-encore-bundle": "^2.1.1",
Expand Down
5 changes: 5 additions & 0 deletions src/Turbo/templates/components/Stream/After.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% props target -%}

<turbo-stream action="after" targets="{{ target }}" {{ attributes }}>
<template>{% block content %}{% endblock %}</template>
</turbo-stream>
5 changes: 5 additions & 0 deletions src/Turbo/templates/components/Stream/Append.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% props target -%}

<turbo-stream action="append" targets="{{ target }}" {{ attributes }}>
<template>{% block content %}{% endblock %}</template>
</turbo-stream>
5 changes: 5 additions & 0 deletions src/Turbo/templates/components/Stream/Before.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% props target -%}

<turbo-stream action="before" targets="{{ target }}" {{ attributes }}>
<template>{% block content %}{% endblock %}</template>
</turbo-stream>
5 changes: 5 additions & 0 deletions src/Turbo/templates/components/Stream/Prepend.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% props target -%}

<turbo-stream action="prepend" targets="{{ target }}" {{ attributes }}>
<template>{% block content %}{% endblock %}</template>
</turbo-stream>
3 changes: 3 additions & 0 deletions src/Turbo/templates/components/Stream/Refresh.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% props requestId = null -%}

<turbo-stream action="refresh"{% if requestId is not null %} request-id="{{ requestId }}"{% endif %} {{ attributes }}></turbo-stream>
3 changes: 3 additions & 0 deletions src/Turbo/templates/components/Stream/Remove.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% props target -%}

<turbo-stream action="remove" targets="{{ target }}" {{ attributes }}></turbo-stream>
5 changes: 5 additions & 0 deletions src/Turbo/templates/components/Stream/Replace.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% props target, morph = false -%}

<turbo-stream action="replace" targets="{{ target }}"{% if morph %} method="morph"{% endif %} {{ attributes }}>
<template>{% block content %}{% endblock %}</template>
</turbo-stream>
5 changes: 5 additions & 0 deletions src/Turbo/templates/components/Stream/Update.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% props target, morph = false -%}

<turbo-stream action="update" targets="{{ target }}"{% if morph %} method="morph"{% endif %} {{ attributes }}>
<template>{% block content %}{% endblock %}</template>
</turbo-stream>
7 changes: 7 additions & 0 deletions src/Turbo/tests/app/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\UX\StimulusBundle\StimulusBundle;
use Symfony\UX\Turbo\TurboBundle;
use Symfony\UX\TwigComponent\TwigComponentBundle;
use Symfony\WebpackEncoreBundle\WebpackEncoreBundle;
use Twig\Environment;

Expand All @@ -54,6 +55,7 @@ public function registerBundles(): iterable
yield new DoctrineBundle();
yield new TwigBundle();
yield new MercureBundle();
yield new TwigComponentBundle();
yield new TurboBundle();
yield new WebpackEncoreBundle();
yield new StimulusBundle();
Expand Down Expand Up @@ -120,6 +122,11 @@ protected function configureContainer(ContainerConfigurator $container): void
],
],
]);

$container->extension('twig_component', [
'anonymous_template_directory' => 'components/',
'defaults' => ['App\Twig\Components\\' => 'components/'],
]);
}

protected function configureRoutes(RoutingConfigurator $routes): void
Expand Down
18 changes: 7 additions & 11 deletions src/Turbo/tests/app/templates/broadcast/Artist.stream.html.twig
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
{% block create %}
<turbo-stream action="append" target="artists">
<template>
<div id="{{ 'artist_' ~ id }}"><a href="{{ path('artist', {id: id}) }}">{{ entity.name }} (#{{ id }})</a></div>
</template>
</turbo-stream>
<twig:Turbo:Stream:Append target="#artists">
<div id="artist_{{ id }}"><a href="{{ path('artist', {id: id}) }}">{{ entity.name }} (#{{ id }})</a></div>
</twig:Turbo:Stream:Append>
{% endblock %}

{% block update %}
<turbo-stream action="update" target="artist_{{ id }}">
<template>
{{ entity.name }} (#{{ id }}, updated)
</template>
</turbo-stream>
<twig:Turbo:Stream:Update target="#artist_{{ id }}">
{{ entity.name }} (#{{ id }}, updated)
</twig:Turbo:Stream:Update>
{% endblock %}

{% block remove %}
<turbo-stream action="remove" target="artist_{{ id }}"></turbo-stream>
<twig:Turbo:Stream:Remove target="#artist_{{ id }}"></twig:Turbo:Stream:Remove>
{% endblock %}
18 changes: 7 additions & 11 deletions src/Turbo/tests/app/templates/broadcast/Book.stream.html.twig
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
{% block create %}
<turbo-stream action="append" target="books">
<template>
<div id="{{ 'book_' ~ id }}">{{ entity.title }} (#{{ id }})</div>
</template>
</turbo-stream>
<twig:Turbo:Stream:Append target="#books">
<div id="book_{{ id }}">{{ entity.title }} (#{{ id }})</div>
</twig:Turbo:Stream:Append>
{% endblock %}

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

{% block remove %}
<turbo-stream action="remove" target="book_{{ id }}"></turbo-stream>
<twig:Turbo:Stream:Remove target="#book_{{ id }}"></twig:Turbo:Stream:Remove>
{% endblock %}
18 changes: 7 additions & 11 deletions src/Turbo/tests/app/templates/broadcast/Song.stream.html.twig
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
{% block create %}
<turbo-stream action="append" target="songs">
<template>
<div id="{{ 'song_' ~ id }}">{{ entity.title }} (#{{ id }}){% if entity.artist %} by {{ entity.artist.name }} (#{{ entity.artist.id }}){% endif %}</div>
</template>
</turbo-stream>
<twig:Turbo:Stream:Append target="#songs">
<div id="song_{{ id }}">{{ entity.title }} (#{{ id }}){% if entity.artist %} by {{ entity.artist.name }} (#{{ entity.artist.id }}){% endif %}</div>
</twig:Turbo:Stream:Append>
{% endblock %}

{% block update %}
<turbo-stream action="update" target="song_{{ id }}">
<template>
{{ entity.title }} (#{{ id }}, updated)
</template>
</turbo-stream>
<twig:Turbo:Stream:Update target="#song_{{ id }}">
{{ entity.title }} (#{{ id }}, updated)
</twig:Turbo:Stream:Update>
{% endblock %}

{% block remove %}
<turbo-stream action="remove" target="song_{{ id }}"></turbo-stream>
<twig:Turbo:Stream:Remove target="#song_{{ id }}"></twig:Turbo:Stream:Remove>
{% endblock %}
8 changes: 3 additions & 5 deletions src/Turbo/tests/app/templates/chat/message.stream.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<turbo-stream action="append" target="messages">
<template>
<div>{{ message }}</div>
</template>
</turbo-stream>
<twig:Turbo:Stream:Append target="#messages">
<div>{{ message }}</div>
</twig:Turbo:Stream:Append>
20 changes: 8 additions & 12 deletions src/Turbo/tests/app/templates/form.stream.html.twig
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<turbo-stream action="replace" target="form">
<template>
<div id="form">
This div replaces the existing element with the DOM ID "form".
</div>
</template>
</turbo-stream>
<twig:Turbo:Stream:Replace target="#form">
<div id="form">
This div replaces the existing element with the DOM ID "form".
</div>
</twig:Turbo:Stream:Replace>

<turbo-stream action="append" target="another_block">
<template>
<div>Appended!</div>
</template>
</turbo-stream>
<twig:Turbo:Stream:Append target="#another_block">
<div>Appended!</div>
</twig:Turbo:Stream:Append>