Skip to content

Commit 62eac54

Browse files
[FrameworkBundle] Add AbstractController::renderBlock() and renderBlockView()
1 parent cfe1e2a commit 62eac54

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

templates.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,51 @@ to define the template to render::
604604

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

607+
The ``AbstractController`` also provides the
608+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController::renderBlock`
609+
and :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController::renderBlockView`
610+
methods::
611+
612+
// src/Controller/ProductController.php
613+
namespace App\Controller;
614+
615+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
616+
use Symfony\Component\HttpFoundation\Response;
617+
618+
class ProductController extends AbstractController
619+
{
620+
// ...
621+
622+
public function price(): Response
623+
{
624+
// ...
625+
626+
// the `renderBlock()` method returns a `Response` object with the
627+
// block contents
628+
return $this->renderBlock('product/index.html.twig', 'price_block', [
629+
// ...
630+
]);
631+
632+
// the `renderBlockView()` method only returns the contents created by the
633+
// template block, so you can use those contents later in a `Response` object
634+
$contents = $this->renderBlockView('product/index.html.twig', 'price_block', [
635+
// ...
636+
]);
637+
638+
return new Response($contents);
639+
}
640+
}
641+
642+
This might come handy when dealing with :ref:`templates inheritance <template_inheritance-layouts>`
643+
or when using `Turbo Streams`_.
644+
645+
.. versionadded:: 6.4
646+
647+
The
648+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController::renderBlock` and
649+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController::renderBlockView`
650+
methods were introduced in Symfony 6.4.
651+
607652
Rendering a Template in Services
608653
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
609654

@@ -1131,6 +1176,8 @@ Use the ``attributes`` option to define the value of hinclude.js options:
11311176
set this option to 'true' to run that JavaScript code #}
11321177
{{ render_hinclude(controller('...'), {attributes: {evaljs: 'true'}}) }}
11331178
1179+
.. _template_inheritance-layouts:
1180+
11341181
Template Inheritance and Layouts
11351182
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11361183

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

0 commit comments

Comments
 (0)