Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

Template extending is done with {% extends 'bundle:folder:template' %} but what if we have complex folder structure. This is where {{ parent() }} comes in handy.


Current folder structure


sport
app
Resources
views
default
base.html.twig
src
Football
BackendBundle
....
FrontendBundle
Resources
views
Country
list.html.twig
index.html.twig
Default
index.html.twig

Extending templates


Pay attention to {{ parent() }} lines.


# sport/app/Resources/views/base.html.twig
<body>
<a href="{{ path('football_frontend_default_index') }}">FRONTEND</a>
 ‐ 
<a href="{{ path('football_backend_default_index') }}">BACKEND</a>
<hr />
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>

# sport/src/Football/FrontendBundle/Resources/views/Default/index.html.twig
{% extends '::base.html.twig' %}

{% block body %}
{% spaceless %}
<a href="{{ path('football_frontend_country_index') }}">COUNTRY</a>
 | 
<a href="{{ path('football_frontend_country_index') }}">LEAGUE</a>
<hr />
{% endspaceless %}
{% endblock %}

# sport/src/Football/FrontendBundle/Resources/views/Country/index.html.twig
{% extends 'FootballFrontendBundle:Default:index.html.twig' %}

{% block body %}
{% spaceless %}
{{ parent() }}
<a href="{{ path('football_frontend_country_index') }}">Index</a>
 | 
<a href="{{ path('football_frontend_country_list') }}">List</a>
 | 
<a href="{{ path('football_frontend_country_create') }}">Create</a>
<hr />
{% endspaceless %}
{% endblock %}

# sport/src/Football/FrontendBundle/Resources/views/Country/list.html.twig
{% extends 'FootballFrontendBundle:Country:index.html.twig' %}

{% block body %}
{% spaceless %}
{{ parent() }}
COUNTRY - List
{% endspaceless %}
{% endblock %}

Output


FRONTEND - BACKEND
-------------------------
COUNTRY | LEAGUE
-------------------------
Index | List | Create
-------------------------
COUNTRY - List