Skip to content

Commit 8ca08d4

Browse files
committed
remove the __props from the component context
1 parent 931f23e commit 8ca08d4

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/TwigComponent/src/ComponentRenderer.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,18 @@ private function preRender(MountedComponent $mounted, array $context = []): PreR
112112
$variables = array_merge(
113113
// first so values can be overridden
114114
$context,
115-
115+
// add the context in a separate variable to keep track
116+
// of what is coming from outside the component
117+
['__context' => $context],
116118
// keep reference to old context
117119
['outerScope' => $context],
118-
119120
// add the component as "this"
120121
['this' => $component],
121-
122122
// add computed properties proxy
123123
['computed' => new ComputedPropertiesProxy($component)],
124124
$props,
125125
// add attributes
126126
[$metadata->getAttributesVar() => $mounted->getAttributes()],
127-
['__props' => array_merge($mounted->getInputProps(), $props)]
128127
);
129128
$event = new PreRenderEvent($mounted, $metadata, $variables);
130129

src/TwigComponent/src/Twig/PropsNode.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function compile(Compiler $compiler): void
3737
$compiler
3838
->write('$propsNames[] = \''.$name.'\';')
3939
->write('$context[\'attributes\'] = $context[\'attributes\']->remove(\''.$name.'\');')
40-
->write('if (!isset($context[\'__props\'][\''.$name.'\'])) {');
40+
->write('if (!isset($context[\''.$name.'\'])) {');
4141

4242
if (!$this->hasNode($name)) {
4343
$compiler
@@ -66,5 +66,25 @@ public function compile(Compiler $compiler): void
6666
->write('}')
6767
->write('}')
6868
;
69+
70+
// overwrite the context value if a props with a similar name and a default value exist
71+
if ($this->hasNode($name)) {
72+
$compiler
73+
->write('if (isset($context[\'__context\'][\''.$name.'\'])) {')
74+
->raw("\n")
75+
->write('$contextValue = $context[\'__context\'][\''.$name.'\'];')
76+
->raw("\n")
77+
->write('$propsValue = $context[\''.$name.'\'];')
78+
->raw("\n")
79+
->write('if ($contextValue === $propsValue) {')
80+
->raw("\n")
81+
->write('$context[\''.$name.'\'] = ')
82+
->subcompile($this->getNode($name))
83+
->raw(";\n")
84+
->write('}')
85+
->raw("\n")
86+
->write('}')
87+
;
88+
}
6989
}
7090
}

0 commit comments

Comments
 (0)