Skip to content

Commit d883d56

Browse files
smnandrejaviereguiluz
authored andcommitted
[Templating] [Template] Render a block with the #[Template] attribute
1 parent f1c7127 commit d883d56

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

templates.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,31 @@ This might come handy when dealing with blocks in
632632
:ref:`templates inheritance <template_inheritance-layouts>` or when using
633633
`Turbo Streams`_.
634634

635+
Similarly, you can use the ``#[Template]`` attribute on the controller to specify a block
636+
to render::
637+
638+
// src/Controller/ProductController.php
639+
namespace App\Controller;
640+
641+
use Symfony\Bridge\Twig\Attribute\Template;
642+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
643+
use Symfony\Component\HttpFoundation\Response;
644+
645+
class ProductController extends AbstractController
646+
{
647+
#[Template('product.html.twig', block: 'price_block')]
648+
public function price(): array
649+
{
650+
return [
651+
// ...
652+
];
653+
}
654+
}
655+
656+
.. versionadded:: 7.2
657+
658+
The ``#[Template]`` attribute's ``block`` argument was introduced in Symfony 7.2.
659+
635660
Rendering a Template in Services
636661
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
637662

0 commit comments

Comments
 (0)