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

[TwigComponent] ingore content when printed as string #1782

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/TwigComponent/src/Twig/TwigPreLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ public function preLexComponents(string $input): string
}
}

// ignore content when printed as string, see #1021
if ($this->consume('{{ "')) {
$output .= $this->consumeUntil('" }}');
$this->consume('" }}');

if ($this->position === $this->length) {
break;
}
}

if ($this->consume('{% embed')) {
$inTwigEmbed = true;
$output .= '{% embed';
Expand Down
9 changes: 9 additions & 0 deletions src/TwigComponent/tests/Unit/TwigPreLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,5 +366,14 @@ public static function getLexTests(): iterable
'<twig:foobar bar="baz" {{ ...attr }}>content</twig:foobar>',
'{% component \'foobar\' with { bar: \'baz\', ...attr } %}{% block content %}content{% endblock %}{% endcomponent %}',
];
yield 'ignore_printed_component' => [
'{{ "<twig:Alert/>" }} <twig:Alert/>',
'<twig:Alert/> {{ component(\'Alert\') }}',
];

yield 'file_ended_with_printed_component' => [
'{{ "<twig:Alert/>" }}',
'<twig:Alert/>',
];
}
}