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] Unexpected Parsing Behavior When Rendering Twig Component Syntax #2606

Open
ThibaultPelloquin opened this issue Feb 26, 2025 · 3 comments
Labels
Status: Waiting Feedback Needs feedback from the author TwigComponent

Comments

@ThibaultPelloquin
Copy link
Contributor

Hello,

I have noticed a behavior that seems unexpected while trying to develop an interactive documentation using Twig in a project that utilizes UX Twig Components.

Versions affected

  • twig/twig: v3.19.0
  • symfony/ux-twig-component: v2.23.0
  • symfony: 7.2.3

How to reproduce

  1. Create a Twig template with the following content:
    <pre><code>{{ '<twig:DuckComponent name="Donald" color="white" quack="quack" />'|escape }}</code></pre>
  2. Render the template.

What I expected

I expected the output to be a code block displaying:

<!-- but escaped -->
<pre><code><twig:DuckComponent name="Donald" color="white" quack="quack" /></code></pre>

What actually happens

Instead of rendering this expected string, I got this error:

Unexpected token "name" of value "DuckComponent" ("end of print statement" expected).

Stack trace:

at templates/dev/assets.html.twig:81
at Twig\TokenStream->expect(4)
(vendor/twig/twig/src/Parser.php:159)
at Twig\Parser->subparse(array(object(BlockTokenParser), 'decideBlockEnd'), true)
(vendor/twig/twig/src/TokenParser/BlockTokenParser.php:46)
at Twig\TokenParser\BlockTokenParser->parse(object(Token))
(vendor/twig/twig/src/Parser.php:202)
at Twig\Parser->subparse(null, false)
(vendor/twig/twig/src/Parser.php:95)
at Twig\Parser->parse(object(TokenStream))
(vendor/twig/twig/src/Environment.php:559)
at Twig\Environment->parse(object(TokenStream))
(vendor/twig/twig/src/Environment.php:590)
at Twig\Environment->compileSource(object(Source))
(vendor/twig/twig/src/Environment.php:409)
at Twig\Environment->loadTemplate('__TwigTemplate_6997921852dc7abcb36b116278776a66', 'dev/assets.html.twig')
(vendor/twig/twig/src/Environment.php:371)
at Twig\Environment->load('dev/assets.html.twig')
(vendor/twig/twig/src/Environment.php:334)

Workaround

If I escape th colon (:), in the component name, it prevents the error and renders what I expect:

<pre><code>{{ '<twig\:DuckComponent name="Donald" color="white" quack="quack" />'|escape }}</code></pre>

... but ...

Twig displays now a Deprecation:

User Deprecated: Since twig/twig 3.12: Character ":" should not be escaped; the "\" character is ignored in Twig 3 but will not be in Twig 4. Please remove the extra "\" character at position 7 in "dev/assets.html.twig" at line 96.

Question

Is my first test the true expected behavior?

Why does Twig attempt to interpret a string inside {{ ... }} as an actual Twig component instead of treating it as plain text?

Thank you so much for your answer. :-)

@ThibaultPelloquin ThibaultPelloquin added the Bug Bug Fix label Feb 26, 2025
@carsonbot carsonbot added the Status: Needs Review Needs to be reviewed label Feb 26, 2025
@smnandre
Copy link
Member

Because TwigComponent use a PreLexer that comes before the Twig Lexer, in order to transliterate the HTML Syntax into classic Twig.

You can try this maybe ?

<pre><code>
    {{ '<' ~ 'twig:Alert message="lala" />' }}
</code></pre>

@smnandre smnandre added TwigComponent Status: Waiting Feedback Needs feedback from the author and removed Bug Bug Fix Status: Needs Review Needs to be reviewed labels Feb 26, 2025
@smnandre smnandre changed the title Unexpected Parsing Behavior When Rendering Twig Component Syntax [TwigComponent] Unexpected Parsing Behavior When Rendering Twig Component Syntax Feb 26, 2025
@ThibaultPelloquin
Copy link
Contributor Author

Hi!

Sorry for the delayed response.

With ~, the issue remains exactly the same.

For the moment, the only solution I have is to replace the < by &lt;.

This solution is not really satisfying, but I haven't found anything better... writing interactive documentation has become really complicated. --'

@smnandre
Copy link
Member

smnandre commented Apr 2, 2025

Hmm it's weird, I just tried again and I have no problem with this

<pre><code>
{{ '<' ~ 'twig:Alert message="lala" />' }}
</code></pre>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Waiting Feedback Needs feedback from the author TwigComponent
Projects
None yet
Development

No branches or pull requests

3 participants