Skip to content

Commit 084e5d3

Browse files
WebMambajaviereguiluz
authored andcommitted
fix null props not overwrite parent context
1 parent bdd3107 commit 084e5d3

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

src/TwigComponent/src/Twig/PropsNode.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,23 @@ public function compile(Compiler $compiler): void
7373
->raw(";\n")
7474
->outdent()
7575
->write('}')
76-
->write("\n");
76+
->write("\n")
77+
;
78+
79+
// overwrite the context value if a props with a similar name and a default value exist
80+
if ($this->hasNode($name)) {
81+
$compiler
82+
->write('if (isset($context[\'__context\'][\''.$name.'\'])) {')
83+
->raw("\n")
84+
->indent()
85+
->write('$context[\''.$name.'\'] = ')
86+
->subcompile($this->getNode($name))
87+
->raw(";\n")
88+
->outdent()
89+
->write('}')
90+
->raw("\n")
91+
;
92+
}
7793
}
7894

7995
$compiler
@@ -94,20 +110,5 @@ public function compile(Compiler $compiler): void
94110
->write('}')
95111
->raw("\n")
96112
;
97-
98-
// overwrite the context value if a props with a similar name and a default value exist
99-
if ($this->hasNode($name)) {
100-
$compiler
101-
->write('if (isset($context[\'__context\'][\''.$name.'\'])) {')
102-
->raw("\n")
103-
->indent()
104-
->write('$context[\''.$name.'\'] = ')
105-
->subcompile($this->getNode($name))
106-
->raw(";\n")
107-
->outdent()
108-
->write('}')
109-
->raw("\n")
110-
;
111-
}
112113
}
113114
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<twig:Alert size='md'>
2+
<twig:Icon/>
3+
</twig:Alert>

src/TwigComponent/tests/Fixtures/templates/components/Alert.html.twig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131
}) %}
3232

3333
<div class="{{ alert.apply({color, size}, attributes.render('class'), 'flex p-4') }}">
34-
...
34+
{% block content %}
35+
{% endblock %}
3536
</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{% props size = null %}
2+
3+
<p>I am an icon</p>
4+
{% if size is not null %}
5+
<p>I am {{ size }}</p>
6+
{% endif %}

src/TwigComponent/tests/Integration/ComponentExtensionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,14 @@ public function testComponentWithConflictBetweenPropsFromTemplateAndClass(): voi
366366
self::getContainer()->get(Environment::class)->render('component_with_conflict_between_props_from_template_and_class.html.twig');
367367
}
368368

369+
public function testAnonymousComponentWithPropsOverwriteParentsProps(): void
370+
{
371+
$output = self::getContainer()->get(Environment::class)->render('anonymous_component_with_props_overwrite_parents_props.html.twig');
372+
373+
$this->assertStringContainsString('I am an icon', $output);
374+
$this->assertStringNotContainsString('I am md', $output);
375+
}
376+
369377
private function renderComponent(string $name, array $data = []): string
370378
{
371379
return self::getContainer()->get(Environment::class)->render('render_component.html.twig', [

0 commit comments

Comments
 (0)