Skip to content

Commit

Permalink
bug #1989 fix null props not overwrite parent context (WebMamba)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 2.x branch.

Discussion
----------

fix null props not overwrite parent context

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| Issues        | Fix #1970
| License       | MIT

So weird! The code was there but at the wrong place!

Commits
-------

084e5d3 fix null props not overwrite parent context
  • Loading branch information
javiereguiluz committed Jul 18, 2024
2 parents fb040a4 + 084e5d3 commit 7e946b7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
33 changes: 17 additions & 16 deletions src/TwigComponent/src/Twig/PropsNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,23 @@ public function compile(Compiler $compiler): void
->raw(";\n")
->outdent()
->write('}')
->write("\n");
->write("\n")
;

// overwrite the context value if a props with a similar name and a default value exist
if ($this->hasNode($name)) {
$compiler
->write('if (isset($context[\'__context\'][\''.$name.'\'])) {')
->raw("\n")
->indent()
->write('$context[\''.$name.'\'] = ')
->subcompile($this->getNode($name))
->raw(";\n")
->outdent()
->write('}')
->raw("\n")
;
}
}

$compiler
Expand All @@ -94,20 +110,5 @@ public function compile(Compiler $compiler): void
->write('}')
->raw("\n")
;

// overwrite the context value if a props with a similar name and a default value exist
if ($this->hasNode($name)) {
$compiler
->write('if (isset($context[\'__context\'][\''.$name.'\'])) {')
->raw("\n")
->indent()
->write('$context[\''.$name.'\'] = ')
->subcompile($this->getNode($name))
->raw(";\n")
->outdent()
->write('}')
->raw("\n")
;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<twig:Alert size='md'>
<twig:Icon/>
</twig:Alert>
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
}) %}

<div class="{{ alert.apply({color, size}, attributes.render('class'), 'flex p-4') }}">
...
{% block content %}
{% endblock %}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% props size = null %}

<p>I am an icon</p>
{% if size is not null %}
<p>I am {{ size }}</p>
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,14 @@ public function testComponentWithConflictBetweenPropsFromTemplateAndClass(): voi
self::getContainer()->get(Environment::class)->render('component_with_conflict_between_props_from_template_and_class.html.twig');
}

public function testAnonymousComponentWithPropsOverwriteParentsProps(): void
{
$output = self::getContainer()->get(Environment::class)->render('anonymous_component_with_props_overwrite_parents_props.html.twig');

$this->assertStringContainsString('I am an icon', $output);
$this->assertStringNotContainsString('I am md', $output);
}

private function renderComponent(string $name, array $data = []): string
{
return self::getContainer()->get(Environment::class)->render('render_component.html.twig', [
Expand Down

0 comments on commit 7e946b7

Please sign in to comment.