New in Symfony 7.2: Template DX Improvements
October 30, 2024 • Published by Javier Eguiluz
Symfony 7.2 is backed by:
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
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
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
{
// ...
}
}
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.