Skip to content

Commit 623fe50

Browse files
committed
feature #2227 Add <twig:Turbo:Stream:*> components (nicolas-grekas)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- Add `<twig:Turbo:Stream:*>` components | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Issues | - | License | MIT This PR provides Twig components for turbo-streams: ```html <twig:Turbo:Stream:Append target="#some-id"> <div>Append this</div> </twig:Turbo:Stream:Append> ``` Sibling PR to #2196 See #2196 (comment) for some background. Commits ------- bfdbb46 CI love 356dbc2 Add `<twig:Turbo:Stream:*>` components
2 parents bca5384 + bfdbb46 commit 623fe50

17 files changed

+88
-53
lines changed

.github/workflows/test-turbo.yml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ on:
88
paths:
99
- 'src/Turbo/**'
1010

11+
env:
12+
SYMFONY_REQUIRE: '>=5.4'
13+
1114
jobs:
1215
phpstan:
1316
runs-on: ubuntu-latest
@@ -38,11 +41,11 @@ jobs:
3841
strategy:
3942
fail-fast: false
4043
matrix:
41-
php-version: ['8.1', '8.3']
44+
php-version: ['8.1', '8.4']
4245
include:
4346
- php-version: '8.1'
4447
dependency-version: 'lowest'
45-
- php-version: '8.3'
48+
- php-version: '8.4'
4649
dependency-version: 'highest'
4750

4851
services:
@@ -69,6 +72,9 @@ jobs:
6972
with:
7073
php-version: ${{ matrix.php-version }}
7174

75+
- name: Install symfony/flex
76+
run: composer global config allow-plugins.symfony/flex true && composer global require symfony/flex
77+
7278
- name: Install Turbo packages
7379
uses: ramsey/composer-install@v3
7480
with:
@@ -101,6 +107,8 @@ jobs:
101107

102108
- name: Run tests
103109
working-directory: src/Turbo
104-
run: vendor/bin/simple-phpunit
110+
run: |
111+
[ 'lowest' = '${{ matrix.dependency-version }}' ] && export SYMFONY_DEPRECATIONS_HELPER=weak
112+
vendor/bin/simple-phpunit
105113
env:
106114
SYMFONY_DEPRECATIONS_HELPER: 'max[self]=1'

src/Turbo/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

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

89
## 2.19.0
910

src/Turbo/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"symfony/property-access": "^5.4|^6.0|^7.0",
5353
"symfony/security-core": "^5.4|^6.0|^7.0",
5454
"symfony/stopwatch": "^5.4|^6.0|^7.0",
55+
"symfony/ux-twig-component": "^2.21",
5556
"symfony/twig-bundle": "^5.4|^6.0|^7.0",
5657
"symfony/web-profiler-bundle": "^5.4|^6.0|^7.0",
5758
"symfony/webpack-encore-bundle": "^2.1.1",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% props target -%}
2+
3+
<turbo-stream action="after" targets="{{ target }}" {{ attributes }}>
4+
<template>{% block content %}{% endblock %}</template>
5+
</turbo-stream>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% props target -%}
2+
3+
<turbo-stream action="append" targets="{{ target }}" {{ attributes }}>
4+
<template>{% block content %}{% endblock %}</template>
5+
</turbo-stream>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% props target -%}
2+
3+
<turbo-stream action="before" targets="{{ target }}" {{ attributes }}>
4+
<template>{% block content %}{% endblock %}</template>
5+
</turbo-stream>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% props target -%}
2+
3+
<turbo-stream action="prepend" targets="{{ target }}" {{ attributes }}>
4+
<template>{% block content %}{% endblock %}</template>
5+
</turbo-stream>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% props requestId = null -%}
2+
3+
<turbo-stream action="refresh"{% if requestId is not null %} request-id="{{ requestId }}"{% endif %} {{ attributes }}></turbo-stream>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% props target -%}
2+
3+
<turbo-stream action="remove" targets="{{ target }}" {{ attributes }}></turbo-stream>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% props target, morph = false -%}
2+
3+
<turbo-stream action="replace" targets="{{ target }}"{% if morph %} method="morph"{% endif %} {{ attributes }}>
4+
<template>{% block content %}{% endblock %}</template>
5+
</turbo-stream>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% props target, morph = false -%}
2+
3+
<turbo-stream action="update" targets="{{ target }}"{% if morph %} method="morph"{% endif %} {{ attributes }}>
4+
<template>{% block content %}{% endblock %}</template>
5+
</turbo-stream>

src/Turbo/tests/app/Kernel.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
3939
use Symfony\UX\StimulusBundle\StimulusBundle;
4040
use Symfony\UX\Turbo\TurboBundle;
41+
use Symfony\UX\TwigComponent\TwigComponentBundle;
4142
use Symfony\WebpackEncoreBundle\WebpackEncoreBundle;
4243
use Twig\Environment;
4344

@@ -54,6 +55,7 @@ public function registerBundles(): iterable
5455
yield new DoctrineBundle();
5556
yield new TwigBundle();
5657
yield new MercureBundle();
58+
yield new TwigComponentBundle();
5759
yield new TurboBundle();
5860
yield new WebpackEncoreBundle();
5961
yield new StimulusBundle();
@@ -120,6 +122,11 @@ protected function configureContainer(ContainerConfigurator $container): void
120122
],
121123
],
122124
]);
125+
126+
$container->extension('twig_component', [
127+
'anonymous_template_directory' => 'components/',
128+
'defaults' => ['App\Twig\Components\\' => 'components/'],
129+
]);
123130
}
124131

125132
protected function configureRoutes(RoutingConfigurator $routes): void
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
{% block create %}
2-
<turbo-stream action="append" target="artists">
3-
<template>
4-
<div id="{{ 'artist_' ~ id }}"><a href="{{ path('artist', {id: id}) }}">{{ entity.name }} (#{{ id }})</a></div>
5-
</template>
6-
</turbo-stream>
2+
<twig:Turbo:Stream:Append target="#artists">
3+
<div id="artist_{{ id }}"><a href="{{ path('artist', {id: id}) }}">{{ entity.name }} (#{{ id }})</a></div>
4+
</twig:Turbo:Stream:Append>
75
{% endblock %}
86

97
{% block update %}
10-
<turbo-stream action="update" target="artist_{{ id }}">
11-
<template>
12-
{{ entity.name }} (#{{ id }}, updated)
13-
</template>
14-
</turbo-stream>
8+
<twig:Turbo:Stream:Update target="#artist_{{ id }}">
9+
{{ entity.name }} (#{{ id }}, updated)
10+
</twig:Turbo:Stream:Update>
1511
{% endblock %}
1612

1713
{% block remove %}
18-
<turbo-stream action="remove" target="artist_{{ id }}"></turbo-stream>
14+
<twig:Turbo:Stream:Remove target="#artist_{{ id }}"></twig:Turbo:Stream:Remove>
1915
{% endblock %}
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
{% block create %}
2-
<turbo-stream action="append" target="books">
3-
<template>
4-
<div id="{{ 'book_' ~ id }}">{{ entity.title }} (#{{ id }})</div>
5-
</template>
6-
</turbo-stream>
2+
<twig:Turbo:Stream:Append target="#books">
3+
<div id="book_{{ id }}">{{ entity.title }} (#{{ id }})</div>
4+
</twig:Turbo:Stream:Append>
75
{% endblock %}
86

97
{% block update %}
10-
<turbo-stream action="update" target="book_{{ id }}">
11-
<template>
12-
{{ entity.title }} (#{{ id }}, updated)
13-
</template>
14-
</turbo-stream>
8+
<twig:Turbo:Stream:Update target="#book_{{ id }}">
9+
{{ entity.title }} (#{{ id }}, updated)
10+
</twig:Turbo:Stream:Update>
1511
{% endblock %}
1612

1713
{% block remove %}
18-
<turbo-stream action="remove" target="book_{{ id }}"></turbo-stream>
14+
<twig:Turbo:Stream:Remove target="#book_{{ id }}"></twig:Turbo:Stream:Remove>
1915
{% endblock %}
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
{% block create %}
2-
<turbo-stream action="append" target="songs">
3-
<template>
4-
<div id="{{ 'song_' ~ id }}">{{ entity.title }} (#{{ id }}){% if entity.artist %} by {{ entity.artist.name }} (#{{ entity.artist.id }}){% endif %}</div>
5-
</template>
6-
</turbo-stream>
2+
<twig:Turbo:Stream:Append target="#songs">
3+
<div id="song_{{ id }}">{{ entity.title }} (#{{ id }}){% if entity.artist %} by {{ entity.artist.name }} (#{{ entity.artist.id }}){% endif %}</div>
4+
</twig:Turbo:Stream:Append>
75
{% endblock %}
86

97
{% block update %}
10-
<turbo-stream action="update" target="song_{{ id }}">
11-
<template>
12-
{{ entity.title }} (#{{ id }}, updated)
13-
</template>
14-
</turbo-stream>
8+
<twig:Turbo:Stream:Update target="#song_{{ id }}">
9+
{{ entity.title }} (#{{ id }}, updated)
10+
</twig:Turbo:Stream:Update>
1511
{% endblock %}
1612

1713
{% block remove %}
18-
<turbo-stream action="remove" target="song_{{ id }}"></turbo-stream>
14+
<twig:Turbo:Stream:Remove target="#song_{{ id }}"></twig:Turbo:Stream:Remove>
1915
{% endblock %}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
<turbo-stream action="append" target="messages">
2-
<template>
3-
<div>{{ message }}</div>
4-
</template>
5-
</turbo-stream>
1+
<twig:Turbo:Stream:Append target="#messages">
2+
<div>{{ message }}</div>
3+
</twig:Turbo:Stream:Append>
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
<turbo-stream action="replace" target="form">
2-
<template>
3-
<div id="form">
4-
This div replaces the existing element with the DOM ID "form".
5-
</div>
6-
</template>
7-
</turbo-stream>
1+
<twig:Turbo:Stream:Replace target="#form">
2+
<div id="form">
3+
This div replaces the existing element with the DOM ID "form".
4+
</div>
5+
</twig:Turbo:Stream:Replace>
86

9-
<turbo-stream action="append" target="another_block">
10-
<template>
11-
<div>Appended!</div>
12-
</template>
13-
</turbo-stream>
7+
<twig:Turbo:Stream:Append target="#another_block">
8+
<div>Appended!</div>
9+
</twig:Turbo:Stream:Append>

0 commit comments

Comments
 (0)