Twig - render box from included or parent template

Is there a way to render a block from an included or parent template?

I have 3 templates

main.html.twig:

{% include 'navbar.html.twig' %}

<div id="main_content">
    {% block application_content %}
    {% endblock application_content %}
</div>

{% block application_footer %}
    footer
{% endblock application_footer %}

      

navbar.html.twig:

<p>bla bla</p>
{% block navbar_profile_photo %}
    <img src="{{ image }}">
{% endblock navbar_profile_photo %}

      

content.html.twig:

{% extends "main.html.twig" %}

{% block application_content %}
   lorem ipsum

   {% block foo %}
       dolor sit amet
   {% endblock foo %}

{% endblock application_content %}

      

I want to be able to call something like this

$ twig-> loadTemplate ('content.html.twig') → renderBlock ('navbar_profile_photo', array ('image' => 'bar.jpg'))

But I can only get blocks from 'content.html.twig'

$ twig-> LoadTemplate ('content.html.twig') → getBlockNames ();

returns

['application_content', 'foo']

I could add to content.html.twig

{% use 'navbar.html.twig'%}

but in my case I had to do it in many templates with many different use statements

Is there a way to get the final template with all inclusions and extensions and blocks?

+3


source to share


1 answer


In your content.html.twig file, you must add:



{% extends "EverlutionApplicationBundle::main.html.twig" %}
{% block navbar_profile_photo %}
    {{ parent() }}
{% endblock navbar_profile_photo %}
.....

      

0


source







All Articles