symfony / ux

Symfony UX initiative: a JavaScript ecosystem for Symfony
https://ux.symfony.com/
MIT License
842 stars 308 forks source link

[TwigComponent] Allow passing functions / array variables to HTML syntax #1287

Open weaverryan opened 11 months ago

weaverryan commented 11 months ago

Hi!

This is something I keep wanting to do:

<twig:Button
    class="foo-bar">
    {{ stimulus_action('say', 'hello') }}
/>

I know why this doesn't work :). But from a user's perspective, I can't think of why it shouldn't. If we see a Twig {{ where we expect an attribute, then we should translate it effectively into a ...stimulus_action('say', 'hello') so its return value merges into the other props.

smnandre commented 11 months ago
Removed / off-topic The closing `>` line 2 is a mistake.. or you suggest a new closing tag ? Now for the stimulus_action method, it could be a ultra-easy... or a pure hell to implement. The "context" there is the current template, not "in" the Button component (in a Twig-compiled-template point of vue) Could you show an example with attributes, props before/after your suggestion ? I have something in mind but don't want to be "off-topic" :)
smnandre commented 11 months ago

Update your src/Twig/TwigPreLexer.php

and add this block line 209 (before the variadic check)

            if ($this->check('{{ stimulus_') || $this->check('{{stimulus_')) {
                $this->consume('{{');
                $attributes[] = '...' . trim($this->consumeUntil('}}'));
                $this->consume('}}');

                continue;
            }

            // rest of the file

            if ($this->check('{{...') || $this->check('{{ ...')) {
                $this->consume('{{...');

Is this the expected behaviour ?

WebMamba commented 11 months ago

But now twig support the spread operator? I think we can do that at the stimulus_actionfunction level

smnandre commented 11 months ago

I'm not sure to get... how could we handle this inside the function ?

DemonTPx commented 11 months ago

I think I ran into the same problem.

I was trying to put symfony form stuff inside of a component like this:

<twig:ChipGroup {{ block('widget_container_attributes') }}>
    ....
</twig:ChipGroup>

It does not work which makes combining twig components and symfony forms quite difficult...

smnandre commented 11 months ago

You cannot "write content" inside the block tag (as you cannot inside any twig tag).. because it as a syntax.

This block should be on the div inside your template

carsonbot commented 4 months ago

Thank you for this issue. There has not been a lot of activity here for a while. Has this been resolved?

carsonbot commented 4 months ago

Just a quick reminder to make a comment on this. If I don't hear anything I'll close this.

carsonbot commented 4 months ago

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!