-
-
Notifications
You must be signed in to change notification settings - Fork 401
Description
It is currently not possible to override nested blocks inside Symfony UX Twig Components when using template inheritance. Twig only allows overriding top-level blocks, which makes reuse of base component templates across multiple projects difficult.
I would like to propose support for global blocks in Twig Components, which would be treated as top-level blocks in a template for inheritance and rendering purposes.
Example
I have:
View.html.twig(Twig Component)base-section.html.twig(base template)section.html.twig(project-specific extension)
The View and base-section would be in a reusable base repository, which is used across multiple projects. The section template is a project specific implementation of a section.
Section/View.html.twig
view-start
<twig:block name="content">
</twig:block>
view-endbase-section.html.twig
<twig:Section:View>
<twig:block name="content">
<twig:block name="title">
base-title
</twig:block>
</twig:block>
</twig:Section:View>section.html.twig
{% extends 'view/base-section.html.twig' %}
<twig:block name="title">
custom-title
</twig:block>Problem
The title block is nested inside the View component. Twig only allows overriding top-level blocks, so the override in section.html.twig does not work.
Current output of section.html.twig: view-start base-title view-end
Expected output: view-start custom-title view-end
Proposed feature: global blocks
To be able to mark a block as global. This would instruct the template compiler to treat this block as global in the context of inheritance.
base-section.html.twig
<twig:Section:View>
<twig:block name="content">
<twig:block name="title" global>
base-title
</twig:block>
</twig:block>
</twig:Section:View>That would allow to override the section.html.twig, and additionally, it would allow the title block to be rendered directly from a PHP, like this:
/**
* @var TemplateWrapper $template
*/
$template = $this->twig->load('section.html.twig');
$template->renderBlock('title');This currently does not work for nested component blocks. It would be useful e.g. in Graphql, when you want to split the content into multiple parts and request only some of them.
Question
Would it be possible to support this concept in Symfony UX Twig Components?
I am willing to try implementing it myself if would it be simple enough, but I would first like to check:
- Whether this is technically possible
- Whether this aligns with the project’s direction
Thank you!