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

fix null props not overwrite parent context #1989

Merged
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
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
Loading