I have just created the AcmeHelloBundle so I have this template that has been generated:
Hello {{ name }}!
Now, I just want to modify the title of the page that contains “Hello John!”. So as a first attempt I edit base.twig.html and change “Welcome” with “foobar” but the title doesn’t change..
So finally to change the title I do this:
{% extends '::base.html.twig' %}
{% block body %}
Hello {{ name }}!
{% endblock %}
Despite it’s working ok with this code (the changes in the title are showed correctly), related to my first attempt I’m wondering how the template is extending the base.html.twig but doesn’t show the changes that I do on it..
Javi
Following template doesn’t extend any other template:
That’s why changes in base template were not visible to you.
You have to explicitly extend a template (with extends statement). Just like you did in your second code snippet:
There’s no magic here. Twig will do whatever you tell it to do.
Since you’re explicit and nothing special happens behind the scenes you’re not limited to one base template. You have the flexibility of organising your templates the way you want.
Read more about it in the Templating chapter of the Symfony docs. Part about three level inheritance explains good practice in organising your templates.