Skip to content

Commit 26a72bb

Browse files
committed
feature #1023 [TwigComponent] Support ...spread operator with html syntax (norkunas)
This PR was merged into the 2.x branch. Discussion ---------- [TwigComponent] Support ...spread operator with html syntax | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | Fix #845 | License | MIT I think that's a new feature like in twig. Commits ------- 3453c00 [TwigComponent] Support ...spread operator with html syntax
2 parents 02a533a + 3453c00 commit 26a72bb

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

src/TwigComponent/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.11.0
4+
5+
- Support ...spread operator with html syntax (requires Twig 3.7.0 or higher)
6+
37
## 2.9.0
48

59
- The `ComponentAttributes::defaults()` method now accepts any iterable argument.

src/TwigComponent/doc/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,12 @@ normal ``{{ }}`` syntax:
997997
// and pass object, or table, or anything you imagine
998998
<twig:Alert :foo="['col' => ['foo', 'oof']]" />
999999

1000+
To forward attributes to another component, use `{{...}}` spread operator syntax:
1001+
1002+
.. code-block:: html+twig
1003+
1004+
<twig:Alert{{ ...myAttributes }} />
1005+
10001006
Passing Blocks to your Component
10011007
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10021008

src/TwigComponent/src/Twig/TwigPreLexer.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class TwigPreLexer
2222
private int $length;
2323
private int $position = 0;
2424
private int $line;
25-
/** @var array<string: name, bool: hasDefaultBlock> */
25+
/**
26+
* @var array<array{name: string, hasDefaultBlock: bool}>
27+
*/
2628
private array $currentComponents = [];
2729

2830
public function __construct(int $startingLine = 1)
@@ -201,6 +203,15 @@ private function consumeAttributes(string $componentName): string
201203
break;
202204
}
203205

206+
if ($this->check('{{...') || $this->check('{{ ...')) {
207+
$this->consume('{{...');
208+
$this->consume('{{ ...');
209+
$attributes[] = '...'.trim($this->consumeUntil('}}'));
210+
$this->consume('}}');
211+
212+
continue;
213+
}
214+
204215
$isAttributeDynamic = false;
205216

206217
// :someProp="dynamicVar"

src/TwigComponent/tests/Unit/TwigPreLexerTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,5 +216,31 @@ public function getLexTests(): iterable
216216
'{% verbatim %}<twig:Alert/>{% endverbatim %}',
217217
'{% verbatim %}<twig:Alert/>{% endverbatim %}',
218218
];
219+
220+
yield 'component_attr_spreading_self_closing' => [
221+
'<twig:foobar bar="baz"{{...attr}}/>',
222+
'{{ component(\'foobar\', { bar: \'baz\', ...attr }) }}',
223+
];
224+
yield 'component_attr_spreading_self_closing2' => [
225+
'<twig:foobar bar="baz"{{ ...customAttrs }} />',
226+
'{{ component(\'foobar\', { bar: \'baz\', ...customAttrs }) }}',
227+
];
228+
yield 'component_attr_spreading_self_closing3' => [
229+
'<twig:foobar bar="baz" {{...attr }} />',
230+
'{{ component(\'foobar\', { bar: \'baz\', ...attr }) }}',
231+
];
232+
233+
yield 'component_attr_spreading_with_content1' => [
234+
'<twig:foobar bar="baz"{{...attr}}>content</twig:foobar>',
235+
'{% component \'foobar\' with { bar: \'baz\', ...attr } %}{% block content %}content{% endblock %}{% endcomponent %}',
236+
];
237+
yield 'component_attr_spreading_with_content2' => [
238+
'<twig:foobar bar="baz"{{ ...customAttrs }}>content</twig:foobar>',
239+
'{% component \'foobar\' with { bar: \'baz\', ...customAttrs } %}{% block content %}content{% endblock %}{% endcomponent %}',
240+
];
241+
yield 'component_attr_spreading_with_content3' => [
242+
'<twig:foobar bar="baz" {{ ...attr }}>content</twig:foobar>',
243+
'{% component \'foobar\' with { bar: \'baz\', ...attr } %}{% block content %}content{% endblock %}{% endcomponent %}',
244+
];
219245
}
220246
}

0 commit comments

Comments
 (0)