Skip to content

Commit 6282451

Browse files
committed
minor #18766 [FrameworkBundle] Add AbstractController::renderBlock() and renderBlockView() (alexandre-daubois)
This PR was merged into the 6.4 branch. Discussion ---------- [FrameworkBundle] Add `AbstractController::renderBlock()` and `renderBlockView()` Fix #18764 Commits ------- a945941 [FrameworkBundle] Add AbstractController::renderBlock() and renderBlockView()
2 parents 61d7898 + a945941 commit 6282451

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

templates.rst

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,52 @@ 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 blocks in
643+
:ref:`templates inheritance <template_inheritance-layouts>` or when using
644+
`Turbo Streams`_.
645+
646+
.. versionadded:: 6.4
647+
648+
The
649+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController::renderBlock` and
650+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController::renderBlockView`
651+
methods were introduced in Symfony 6.4.
652+
607653
Rendering a Template in Services
608654
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
609655

@@ -1131,6 +1177,8 @@ Use the ``attributes`` option to define the value of hinclude.js options:
11311177
set this option to 'true' to run that JavaScript code #}
11321178
{{ render_hinclude(controller('...'), {attributes: {evaljs: 'true'}}) }}
11331179
1180+
.. _template_inheritance-layouts:
1181+
11341182
Template Inheritance and Layouts
11351183
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11361184

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

0 commit comments

Comments
 (0)