Symfony 7.2 includes some DX (developer experience) improvements to make your work easier in some tasks related to templates.

Define Headers in Static Pages

HypeMC
Contributed by HypeMC in #54678

Although templates are usually rendered in controllers and services, you can also render static pages directly from the route definition. In Symfony 7.2, you can also define the HTTP headers of those pages:

1
2
3
4
5
6
7
8
9
10
11
# config/routes.yaml
releases_char:
    path: /symfony-releases-chart
    controller: Symfony\Bundle\FrameworkBundle\Controller\TemplateController
    defaults:
        template:  'static/releases/chart.svg.twig'
        # ...

        # the HTTP headers added to the response
        headers:
            Content-Type: 'image/svg+xml'

Render Twig Blocks Using Attributes

Simon André
Contributed by Simon André in #58028

If you use Symfony UX Turbo to create fully dynamic interfaces without writing JavaScript, it's common to render Twig template blocks instead of entire Twig templates.

In Symfony 6.4, the renderBlock() and renderBlockView() methods were added to the base AbstractController to simplify block rendering. In Symfony 7.2, we're adding a block option to the #[Template] attribute to render specific blocks:

1
2
3
4
5
6
7
8
9
10
11
use Symfony\Bridge\Twig\Attribute\Template;
// ...

class ProductController extends AbstractController
{
    #[Template('product/listings.html.twig', block: 'top_list')]
    public function topSellers(): array
    {
        // ...
    }
}
Published in #Living on the edge