[TwigComponent] Fix opening of default block inside an open twig block #892
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 acontentblock.Note: the error won't happen if another non-whitespace character is used before the last
<twig:Foo />, because then the transformed output contains acontentblock 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
contentblock. (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.
{% 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.
{% component 'Foo' %} {% block content %}foo_content{% endblock %} {% block foo_block %} foo_block_content {% component 'Foo' %} {% block content %}{{ component('Foo') }}{% endblock %} {% endcomponent %} {% endblock %} {% endcomponent %}