Skip to content

Commit 6df9d6b

Browse files
committed
[TwigComponent] Making self-closing HTML syntax render with simpler {{ component() }}
Avoids a bug where we think that all {% component %} style tags contain blocks, which cause them to not be allowed with live components
1 parent 0e24acb commit 6df9d6b

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/TwigComponent/src/Twig/TwigPreLexer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ public function preLexComponents(string $input): string
5858
$this->currentComponents[] = ['name' => $componentName, 'hasDefaultBlock' => false];
5959
}
6060

61-
$output .= "{% component '{$componentName}'".($attributes ? " with { {$attributes} }" : '').' %}';
6261
if ($isSelfClosing) {
63-
$output .= '{% endcomponent %}';
62+
// use the simpler component() format, so that the system doesn't think
63+
// this is an "embedded" component with blocks
64+
// see https://github.com/symfony/ux/issues/810
65+
$output .= "{{ component('{$componentName}'".($attributes ? ", { {$attributes} }" : '').') }}';
66+
} else {
67+
$output .= "{% component '{$componentName}'".($attributes ? " with { {$attributes} }" : '').' %}';
6468
}
6569

6670
continue;

src/TwigComponent/tests/Unit/TwigPreLexerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ public function getLexTests(): iterable
2929
{
3030
yield 'simple_component' => [
3131
'<twig:foo />',
32-
'{% component \'foo\' %}{% endcomponent %}',
32+
'{{ component(\'foo\') }}',
3333
];
3434

3535
yield 'component_with_attributes' => [
3636
'<twig:foo bar="baz" with_quotes="It\'s with quotes" />',
37-
"{% component 'foo' with { bar: 'baz', with_quotes: 'It\'s with quotes' } %}{% endcomponent %}",
37+
"{{ component('foo', { bar: 'baz', with_quotes: 'It\'s with quotes' }) }}",
3838
];
3939

4040
yield 'component_with_dynamic_attributes' => [
4141
'<twig:foo dynamic="{{ dynamicVar }}" :otherDynamic="anotherVar" />',
42-
'{% component \'foo\' with { dynamic: dynamicVar, otherDynamic: anotherVar } %}{% endcomponent %}',
42+
'{{ component(\'foo\', { dynamic: dynamicVar, otherDynamic: anotherVar }) }}',
4343
];
4444

4545
yield 'component_with_closing_tag' => [
@@ -54,12 +54,12 @@ public function getLexTests(): iterable
5454

5555
yield 'component_with_embedded_component_inside_block' => [
5656
'<twig:foo><twig:block name="foo_block"><twig:bar /></twig:block></twig:foo>',
57-
'{% component \'foo\' %}{% block foo_block %}{% component \'bar\' %}{% endcomponent %}{% endblock %}{% endcomponent %}',
57+
'{% component \'foo\' %}{% block foo_block %}{{ component(\'bar\') }}{% endblock %}{% endcomponent %}',
5858
];
5959

6060
yield 'attribute_with_no_value' => [
6161
'<twig:foo bar />',
62-
'{% component \'foo\' with { bar: true } %}{% endcomponent %}',
62+
'{{ component(\'foo\', { bar: true }) }}',
6363
];
6464

6565
yield 'component_with_default_block_content' => [
@@ -69,7 +69,7 @@ public function getLexTests(): iterable
6969

7070
yield 'component_with_default_block_that_holds_a_component_and_multi_blocks' => [
7171
'<twig:foo>Foo <twig:bar /><twig:block name="other_block">Other block</twig:block></twig:foo>',
72-
'{% component \'foo\' %}{% block content %}Foo {% component \'bar\' %}{% endcomponent %}{% endblock %}{% block other_block %}Other block{% endblock %}{% endcomponent %}',
72+
'{% component \'foo\' %}{% block content %}Foo {{ component(\'bar\') }}{% endblock %}{% block other_block %}Other block{% endblock %}{% endcomponent %}',
7373
];
7474
yield 'component_with_character_:_on_his_name' => [
7575
'<twig:foo:bar></twig:foo:bar>',

0 commit comments

Comments
 (0)