You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
bug #892 [TwigComponent] Fix opening of default block inside an open twig block (sneakyvv)
This PR was merged into the 2.x branch.
Discussion
----------
[TwigComponent] Fix opening of default block inside an open twig block
| 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
<twig:Foo>
<twig:block name="foo_block">
<twig:Foo>
<twig:Foo />
</twig:Foo>
</twig:block>
</twig:Foo>
```
## Problem
This currently renders to
```twig
{% 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:
```twig
{% 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
<twig:Foo>
foo_content
<twig:block name="foo_block">
<twig:Foo>
<twig:Foo />
</twig:Foo>
</twig:block>
</twig:Foo>
```
```twig
{% 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
<twig:Foo>
foo_content
<twig:block name="foo_block">
foo_block_content
<twig:Foo>
<twig:Foo />
</twig:Foo>
</twig:block>
</twig:Foo>
```
```twig
{% component 'Foo' %}
{% block content %}foo_content{% endblock %}
{% block foo_block %}
foo_block_content
{% component 'Foo' %}
{% block content %}{{ component('Foo') }}{% endblock %}
{% endcomponent %}
{% endblock %}
{% endcomponent %}
```
Commits
-------
0c636f4 Fix opening of default block inside an open block
0 commit comments