DEMOS / Turbo

Turbo Infinite Scroll - 1/2

This demo explores the same product grid as the Live Component version, using Turbo Streams: click Load More to append a batch of cards and replace the link with the next page.

ux.symfony.com
💐
49.99
🌸
50.99
🪷
51.99
🏵️
52.99
🌹
53.99
🌺
54.99
🌻
55.99
🌼
56.99
🌷
57.99
🪻
58.99
🌱
59.99
🪴
60.99

So... how does it work?

Try loading two batches. Watch how the link stays at the end of the grid as new cards appear.

Serve the next batch

The page query parameter defaults to 1. The controller fetches 12 cards at a time and works out whether to offer a link to the next page.

The first visit renders the full page. A Turbo Stream request uses the same route, but renderBlock() returns only the actions that update the existing page.

42#[Route('/infinite-scroll', name: 'app_demo_turbo_infinite_scroll')]
43public function infiniteScroll(
44    Request $request,
45    #[MapQueryParameter(filter: \FILTER_VALIDATE_INT, options: ['min_range' => 1])] int $page = 1,
46): Response {
47    $items = $this->getItems($page);
48
49    $hasMore = \count($this->emojiCollection) > ($page * self::PER_PAGE);
50    $nextPage = $hasMore ? $page + 1 : null;
51
52    if (TurboBundle::STREAM_FORMAT === $request->getPreferredFormat()) {
53        $request->setRequestFormat(TurboBundle::STREAM_FORMAT);
54
55        return $this->renderBlock('demos/turbo/infinite_scroll.html.twig', 'stream_success', [
56            'items' => $items,
57            'has_more' => $hasMore,
58            'next_page' => $nextPage,
59        ]);
60    }
61
62    return $this->render('demos/turbo/infinite_scroll.html.twig', [
63        'demo' => $this->turboDemoRepository->find('infinite-scroll'),
64        'relatedLiveDemo' => $this->liveDemoRepository->find('infinite-scroll'),
65        'items' => $items,
66        'has_more' => $hasMore,
67        'next_page' => $nextPage,
68    ]);
69}

Load more is a link to the same route with the next page value. The data-turbo-stream attribute tells Turbo to add the stream media type to the request’s Accept header.

Setting data-turbo-prefetch to false keeps Turbo from sending that request when you hover over the link.

74{% macro load_more(has_more, next_page) %}
75    <div id="product-grid-load-more">
76        {% if has_more %}
77            <a
78                class="ProductGrid_more"
79                href="{{ path('app_demo_turbo_infinite_scroll', {
80                    page: next_page,
81                }) }}"
82                data-turbo-stream="true"
83                data-turbo-prefetch="false"
84            >
85                Load More
86            </a>
87        {% else %}
88            <span class="code">The End</span>
89        {% endif %}
90    </div>
91{% endmacro %}

Turbo:Stream:Append targets #product-grid, so the new cards appear after the existing ones.

A separate Turbo:Stream:Replace targets #product-grid-load-more. The same macro renders the next link inside that wrapper or displays The End when there are no more results.

62{% block stream_success %}
63    <twig:Turbo:Stream:Append target="#product-grid">
64        {% for item in items %}
65            <twig:ProductGridItem :item="item" />
66        {% endfor %}
67    </twig:Turbo:Stream:Append>
68    <twig:Turbo:Stream:Replace target="#product-grid-load-more">
69        {% from 'demos/turbo/infinite_scroll.html.twig' import load_more %}
70        {{ load_more(has_more, next_page) }}
71    </twig:Turbo:Stream:Replace>
72{% endblock %}
Author ker0x
Published 2026-04-20