Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different extends behavior when passing an array to extends #3691

Closed
eXaminator opened this issue Apr 22, 2022 · 1 comment · Fixed by #4337
Closed

Different extends behavior when passing an array to extends #3691

eXaminator opened this issue Apr 22, 2022 · 1 comment · Fixed by #4337

Comments

@eXaminator
Copy link

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 %}
@fabpot
Copy link
Contributor

fabpot commented Sep 24, 2024

Fixed by #3999
Tests added in #4337

@fabpot fabpot closed this as completed Sep 24, 2024
fabpot added a commit that referenced this issue Sep 25, 2024
This PR was merged into the 3.x branch.

Discussion
----------

Add some tests

Closes #3691

The bug described in #3691 has been fixed via #3999.

This PR adds tests to prove it works now and to avoid future regressions.

Commits
-------

85c2ba5 Add some tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants