New in Symfony 7.2: Template DX Improvements
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:
# config/routes.yaml
releases_char:
path: /symfony-releases-chart
controller: SymfonyBundleFrameworkBundleControllerTemplateController
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:
use SymfonyBridgeTwigAttributeTemplate;
// …
class ProductController extends AbstractController
{
(‚product/listings.html.twig‘, block: ‚top_list‘)
public function topSellers(): array
{
// …
}
}
Symfony Blog