Twig block switched to built-in template

I use twig templates in a thin framework. I have a page.phtml template that splits an array of data and has several subblocks designed to be overridden for each element, such as pagination of users, events, orders, etc.

page.phtml

 <div class="page"> {% block block1 %}default content{% endblock %} <ul> ... </ul> {% block block2 %}{% endblock %} </div> 

I have an event.phtml template that includes page.phtml and adds other content to the page; it also overrides the contents of the default page block

event.phtml

 <html> <body> <h1>Event Page</h1> {% embed "page.phtml" %} {% block block1 %}event page content{% endblock %} {% endembed %} </body> </html> 

I have a custom event page that only requires changing a few blocks of event pages, so I extended event.phtml like this

custom_event.phtml

 {% extends "event.phtml" %} {% block block2 %}overridden value{% endblock %} 

and it is expected that the overridden content of block2 will appear in the page.phtml template embedded by the parent template. I can output the value of block2 in the parent template there too, but in the inline template this is not so. I tried explicitly passing the block to event.phtml in the attachment, e.g.

 {%embed "page.phtml" %} {% block block2 %}{{parent()}}{% endblock %} ... {% endembed %} 

But that didn't make any difference. How to get the redefined block2 from the custom_event.phtml template throughout the extended event.phtml template and into the built-in page.phtml template?

+7
extend block templates twig embed
source share
1 answer

You cannot do this.

{% embed %} technically defines a new (anonymous) template that extends the built-in template (so it can overwrite blocks) and is included in the current template. This is really just syntactic sugar for {% include %} and {% extends %} to avoid having to store this partial template in its own file and include it only once.
The inclusion of Twig is not extended by child templates. Only blocks of the current template are available for expansion. And event.phtml has no blocks.

0
source share

All Articles