Twig
Twig copied to clipboard
Different extends behavior when passing an array to extends
Hello,
while updating Twig to version 3 we encountered a problem with template extensions when passing an array of templates to the extends tag.
The problem occurs, when you extend a parent template, which contains two nested blocks and a child template which overrides the outer block and calls the parent() function (i.e. to wrap the contents of the parents outer block), while also overriding the inner block.
If the child extends the parent directly, passing only a string to extends, it woks as expected: The content of the childs inner block is used.
If the child extends the same parent but passes it via an array (because there might be other templates that could be extended instead), the output will contain the parents default value for the inner block instead.
Here is a minimal example that demonstrates the problem: https://twigfiddle.com/itkl51
Notice how the output shows inner default instead of the expected inner actual.
If the extends tag is changed from {% extends ["parent.twig"] %} to {% extends "parent.twig" %}, the output will change to the expected inner actual.
Here's the full example code as reference, in case the above link dies at some point.
{# parent.twig #}
{% block outer %}
outer start
{% block inner %}
inner default
{% endblock %}
outer end
{% endblock %}
{# child.twig #}
{% extends ["parent.twig"] %}
{% block outer %}
outer wrap start
{{ parent() }}
outer wrap end
{% endblock %}
{% block inner %}
inner actual
{% endblock %}