4
votes

is there any way to render block from 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 call something like this

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

But I can get only 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 some way to get the final template with all includes and extends and blocks?

1

1 Answers

0
votes

In content.html.twig file you must add:

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