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 Anonymous component props and Class backed component properties d… #1146

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
7 changes: 4 additions & 3 deletions src/TwigComponent/src/ComponentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ private function preRender(MountedComponent $mounted, array $context = []): PreR

$component = $mounted->getComponent();
$metadata = $this->factory->metadataFor($mounted->getName());
// expose public properties and properties marked with ExposeInTemplate attribute
$props = iterator_to_array($this->exposedVariables($component, $metadata->isPublicPropsExposed()));
$variables = array_merge(
// first so values can be overridden
$context,
Expand All @@ -132,9 +134,8 @@ private function preRender(MountedComponent $mounted, array $context = []): PreR

// add attributes
[$metadata->getAttributesVar() => $mounted->getAttributes()],

// expose public properties and properties marked with ExposeInTemplate attribute
iterator_to_array($this->exposedVariables($component, $metadata->isPublicPropsExposed())),
$props,
['__props' => $props]
);
$event = new PreRenderEvent($mounted, $metadata, $variables);

Expand Down
2 changes: 1 addition & 1 deletion src/TwigComponent/src/Twig/PropsNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function compile(Compiler $compiler): void
$compiler
->write('$propsNames[] = \''.$name.'\';')
->write('$context[\'attributes\'] = $context[\'attributes\']->remove(\''.$name.'\');')
->write('if (!isset($context[\''.$name.'\'])) {');
->write('if (!isset($context[\'__props\'][\''.$name.'\'])) {');

if (!$this->hasNode($name)) {
$compiler
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% set message = 'bar' %}

<twig:Message>
<p>Hey!</p>
</twig:Message>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% props message = 'foo' %}

<p>{{ message }}</p>
{% block content %}
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ public function testRenderAnonymousComponentWithNonScalarProps(): void
$this->assertStringContainsString('class variable defined? no', $output);
}

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

$this->assertStringContainsString('<p>foo</p>', $output);
}

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