Skip to content

[TwigComponent] Fix opening of default block inside an open twig block #892

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

Merged
merged 1 commit into from
Jun 8, 2023

Conversation

sneakyvv
Copy link
Contributor

Q A
Bug fix? yes
New feature? no
Tickets
License MIT

Symptom

SyntaxError when using a Embedded Component inside a block

A template that extends another one cannot include content outside Twig blocks. Did you forget to put the content inside a {% block %} tag?

Reproduce

Embed a component inside a twig block.

<twig:Foo>
    <twig:block name="foo_block">
        <twig:Foo>
            <twig:Foo />
        </twig:Foo>
    </twig:block>
</twig:Foo>

Problem

This currently renders to

{% component 'Foo' %}
    {% block foo_block %}
        {% component 'Foo' %}
            {{ component('Foo') }}
        {% endcomponent %}
    {% endblock %}
{% endcomponent %}

The last {{ component('Foo') }} should be enclosed within a content block.

Note: the error won't happen if another non-whitespace character is used before the last <twig:Foo />, because then the transformed output contains a content block for that. It ony happens when lexing a twig block with a component inside which immediately has another component inside it.

Cause

https://github.com/symfony/ux/blob/2.x/src/TwigComponent/src/Twig/TwigPreLexer.php#L82-L89 checks imo incorrectly whether it's inside a block already. As long as a new component is being processed it's OK to open a new default content block. (see solution)

Solution

This PR will lex the above twig syntax to:

{% component 'Foo' %}
    {% block foo_block %}
        {% component 'Foo' %}
            {% block content %}{{ component('Foo') }}{% endblock %}
        {% endcomponent %}
    {% endblock %}
{% endcomponent %}

Even a content block inside a content block is still OK, because the block is part of another component's template.

<twig:Foo>
    foo_content
    <twig:block name="foo_block">
        <twig:Foo>
            <twig:Foo />
        </twig:Foo>
    </twig:block>
</twig:Foo>
{% component 'Foo' %}
    {% block content %}foo_content{% endblock %}
    {% block foo_block %}
        {% component 'Foo' %}
            {% block content %}{{ component('Foo') }}{% endblock %}
        {% endcomponent %}
    {% endblock %}
{% endcomponent %}

Note that regular block content is (still) NOT enclosed inside a default block.

<twig:Foo>
    foo_content
    <twig:block name="foo_block">
        foo_block_content
        <twig:Foo>
            <twig:Foo />
        </twig:Foo>
    </twig:block>
</twig:Foo>
{% component 'Foo' %}
    {% block content %}foo_content{% endblock %}
    {% block foo_block %}
        foo_block_content
        {% component 'Foo' %}
        {% block content %}{{ component('Foo') }}{% endblock %}
        {% endcomponent %}
    {% endblock %}
{% endcomponent %}

@sneakyvv
Copy link
Contributor Author

@weaverryan Going to tag you here since git blame indicates you added the check at https://github.com/symfony/ux/blob/2.x/src/TwigComponent/src/Twig/TwigPreLexer.php#L82-L89. So perhaps, you can tell if I'm wrong in saying that the $insideOfBlock check shouldn't happen.

@sneakyvv sneakyvv force-pushed the fix-default-block-opening branch from 860fe93 to 79e5540 Compare May 23, 2023 14:37
@sneakyvv sneakyvv force-pushed the fix-default-block-opening branch 2 times, most recently from 21140ec to 0c636f4 Compare May 25, 2023 19:29
@weaverryan
Copy link
Member

I agree it looks weird that you basically undid part of an earlier fix I did, but I wrote good test cases for that bug and you added a new test case here. So very possible I fixed my earlier bug and this code was never needed (and in the weeds, I thought it was). For this complex feature, we've gotta let the tests be our guide. Thanks!

@weaverryan weaverryan merged commit e780fa7 into symfony:2.x Jun 8, 2023
@sneakyvv sneakyvv deleted the fix-default-block-opening branch August 23, 2023 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants