Skip to content

[FrameworkBundle] Add AbstractController::renderBlock() and renderBlockView() #18766

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
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
49 changes: 49 additions & 0 deletions templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,52 @@ to define the template to render::

The ``#[Template()]`` attribute was introduced in Symfony 6.2.

The ``AbstractController`` also provides the
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController::renderBlock`
and :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController::renderBlockView`
methods::

// src/Controller/ProductController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class ProductController extends AbstractController
{
// ...

public function price(): Response
{
// ...

// the `renderBlock()` method returns a `Response` object with the
// block contents
return $this->renderBlock('product/index.html.twig', 'price_block', [
// ...
]);

// the `renderBlockView()` method only returns the contents created by the
// template block, so you can use those contents later in a `Response` object
$contents = $this->renderBlockView('product/index.html.twig', 'price_block', [
// ...
]);

return new Response($contents);
}
}

This might come handy when dealing with blocks in
:ref:`templates inheritance <template_inheritance-layouts>` or when using
`Turbo Streams`_.

.. versionadded:: 6.4

The
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController::renderBlock` and
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController::renderBlockView`
methods were introduced in Symfony 6.4.

Rendering a Template in Services
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -1131,6 +1177,8 @@ Use the ``attributes`` option to define the value of hinclude.js options:
set this option to 'true' to run that JavaScript code #}
{{ render_hinclude(controller('...'), {attributes: {evaljs: 'true'}}) }}

.. _template_inheritance-layouts:

Template Inheritance and Layouts
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -1614,3 +1662,4 @@ for this class and :doc:`tag your service </service_container/tags>` with ``twig
.. _`official Twig extensions`: https://github.com/twigphp?q=extra
.. _`global variables`: https://twig.symfony.com/doc/3.x/advanced.html#id1
.. _`hinclude.js`: https://mnot.github.io/hinclude/
.. _`Turbo Streams`: https://symfony.com/bundles/ux-turbo/current/index.html