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] Support ...spread operator with html syntax #1023

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/TwigComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 2.11.0

- Support ...spread operator with html syntax (requires Twig 3.7.0 or higher)

## 2.9.0

- The `ComponentAttributes::defaults()` method now accepts any iterable argument.
Expand Down
6 changes: 6 additions & 0 deletions src/TwigComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,12 @@ normal ``{{ }}`` syntax:
// and pass object, or table, or anything you imagine
<twig:Alert :foo="['col' => ['foo', 'oof']]" />

To forward attributes to another component, use `{{...}}` spread operator syntax:

.. code-block:: html+twig

<twig:Alert{{ ...myAttributes }} />

Passing Blocks to your Component
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
13 changes: 12 additions & 1 deletion src/TwigComponent/src/Twig/TwigPreLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class TwigPreLexer
private int $length;
private int $position = 0;
private int $line;
/** @var array<string: name, bool: hasDefaultBlock> */
/**
* @var array<array{name: string, hasDefaultBlock: bool}>
*/
private array $currentComponents = [];

public function __construct(int $startingLine = 1)
Expand Down Expand Up @@ -201,6 +203,15 @@ private function consumeAttributes(string $componentName): string
break;
}

if ($this->check('{{...') || $this->check('{{ ...')) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should consume the whitespace before doing this check, tab, or multiples tab can be valid syntax too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is consumed 4 lines before, isn't it?

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

continue;
}

$isAttributeDynamic = false;

// :someProp="dynamicVar"
Expand Down
26 changes: 26 additions & 0 deletions src/TwigComponent/tests/Unit/TwigPreLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,31 @@ public function getLexTests(): iterable
'{% verbatim %}<twig:Alert/>{% endverbatim %}',
'{% verbatim %}<twig:Alert/>{% endverbatim %}',
];

yield 'component_attr_spreading_self_closing' => [
'<twig:foobar bar="baz"{{...attr}}/>',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having ... between {{ why not just do:

<twig:foobar bar="baz" ...attr />

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me eyes it's easier to find that it's a variable like in twig

Copy link
Member

@kbond kbond Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little funky looking but what about <twig:foobar bar="baz" :...attr />? (in addition to {{). This sort of lines up with dynamic attribute values: https://symfony.com/bundles/ux-twig-component/current/index.html#passing-props-as-html-attributes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My proposed solution is already in our prod and working, so I'd prefer as is 😁

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at React:

<button className={className} {...other} />

And Vue:

<button :className="className" v-bind="other">

Given these, the logical options are:

<twig:Button className={{ className }} {{...other}} />

or

<twig:Button live-bind="other">

And that live-bind thing looks silly :). So I agree with the proposed syntax.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that JSX for Vue.js also use {... } syntax.

{{... }} is nice because the two brackets reminds us about Twig

'{{ component(\'foobar\', { bar: \'baz\', ...attr }) }}',
];
yield 'component_attr_spreading_self_closing2' => [
'<twig:foobar bar="baz"{{ ...customAttrs }} />',
'{{ component(\'foobar\', { bar: \'baz\', ...customAttrs }) }}',
];
yield 'component_attr_spreading_self_closing3' => [
'<twig:foobar bar="baz" {{...attr }} />',
'{{ component(\'foobar\', { bar: \'baz\', ...attr }) }}',
];

yield 'component_attr_spreading_with_content1' => [
'<twig:foobar bar="baz"{{...attr}}>content</twig:foobar>',
'{% component \'foobar\' with { bar: \'baz\', ...attr } %}{% block content %}content{% endblock %}{% endcomponent %}',
];
yield 'component_attr_spreading_with_content2' => [
'<twig:foobar bar="baz"{{ ...customAttrs }}>content</twig:foobar>',
'{% component \'foobar\' with { bar: \'baz\', ...customAttrs } %}{% block content %}content{% endblock %}{% endcomponent %}',
];
yield 'component_attr_spreading_with_content3' => [
'<twig:foobar bar="baz" {{ ...attr }}>content</twig:foobar>',
'{% component \'foobar\' with { bar: \'baz\', ...attr } %}{% block content %}content{% endblock %}{% endcomponent %}',
];
}
}
Loading