Skip to content

Commit a669df9

Browse files
committed
Allow more flexible twig expressions as component attributes
1 parent 56d6fb5 commit a669df9

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

src/Twig/ComponentNode.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Twig\Compiler;
66
use Twig\Node\EmbedNode;
7-
use Twig\Node\Expression\ArrayExpression;
7+
use Twig\Node\Expression\AbstractExpression;
88

99
/**
1010
* @author Fabien Potencier <[email protected]>
@@ -16,7 +16,7 @@
1616
*/
1717
final class ComponentNode extends EmbedNode
1818
{
19-
public function __construct(string $component, string $template, int $index, ArrayExpression $variables, bool $only, int $lineno, string $tag)
19+
public function __construct(string $component, string $template, int $index, AbstractExpression $variables, bool $only, int $lineno, string $tag)
2020
{
2121
parent::__construct($template, $index, $variables, $only, false, $lineno, $tag);
2222

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% set attributes1 = {propA: 'A1', propB: 'B1' } %}
2+
{% set attributes3 = {propA: 'A3', propB: 'B3' } %}
3+
4+
{{ component('component_a', attributes1) }}
5+
{{ component('component_a', {propA: 'A2', propB: 'B0' }|merge({propB: 'B2'})) }}
6+
{% component 'component_a' with attributes3 %}{% endcomponent %}
7+
{% component 'component_a' with ({propA: 'A4', propB: 'B0' })|merge({propB: 'B4'}) %}{% endcomponent %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{% set attributes = 'wrong' %}
2+
{{ component('component_a', attributes) }}

tests/Integration/ComponentExtensionTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ public function testCanRenderTheSameComponentMultipleTimes(): void
4444
$this->assertStringContainsString('service: service a value', $output);
4545
}
4646

47+
public function testCanRenderComponentWithMoreAdvancedTwigExpressions(): void
48+
{
49+
$output = self::getContainer()->get(Environment::class)->render('flexible_component_attributes.html.twig');
50+
51+
$this->assertStringContainsString('propA: A1', $output);
52+
$this->assertStringContainsString('propB: B1', $output);
53+
$this->assertStringContainsString('propA: A2', $output);
54+
$this->assertStringContainsString('propB: B2', $output);
55+
$this->assertStringContainsString('propA: A3', $output);
56+
$this->assertStringContainsString('propB: B3', $output);
57+
$this->assertStringContainsString('propA: A4', $output);
58+
$this->assertStringContainsString('propB: B4', $output);
59+
$this->assertStringContainsString('service: service a value', $output);
60+
}
61+
62+
public function testCanNotRenderComponentWithInvalidExpressions(): void
63+
{
64+
$this->expectException(\TypeError::class);
65+
self::getContainer()->get(Environment::class)->render('invalid_flexible_component.html.twig');
66+
}
67+
4768
public function testCanCustomizeTemplateWithAttribute(): void
4869
{
4970
$output = $this->renderComponent('component_b', ['value' => 'b value 1']);

0 commit comments

Comments
 (0)