Skip to content

Commit 6919de0

Browse files
committed
resolve render variables from parent context
1 parent ec6ae93 commit 6919de0

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/Tags/RenderTag.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function stream(RenderContext $context): \Generator
119119
$forLoop = new ForLoopDrop($templateName, count($variable));
120120

121121
foreach ($variable as $value) {
122-
$partialContext = $this->setInnerContextVariables($context->newIsolatedSubContext($templateName), [
122+
$partialContext = $this->buildPartialContext($context, $templateName, [
123123
'forloop' => $forLoop,
124124
$contextVariableName => $value,
125125
]);
@@ -132,7 +132,7 @@ public function stream(RenderContext $context): \Generator
132132
return;
133133
}
134134

135-
$partialContext = $this->setInnerContextVariables($context->newIsolatedSubContext($templateName), [
135+
$partialContext = $this->buildPartialContext($context, $templateName, [
136136
$contextVariableName => $variable,
137137
]);
138138

@@ -162,17 +162,19 @@ protected function loadPartial(RenderContext $context): Template
162162
return $context->loadPartial($templateName, parseIfMissing: $this->allowDynamicPartials());
163163
}
164164

165-
protected function setInnerContextVariables(RenderContext $context, array $variables = []): RenderContext
165+
protected function buildPartialContext(RenderContext $rootContext, string $templateName, array $variables = []): RenderContext
166166
{
167+
$partialContext = $rootContext->newIsolatedSubContext($templateName);
168+
167169
foreach ($variables as $key => $value) {
168-
$context->set($key, $value);
170+
$partialContext->set($key, $value);
169171
}
170172

171173
foreach ($this->attributes as $key => $value) {
172-
$context->set($key, $context->evaluate($value));
174+
$partialContext->set($key, $rootContext->evaluate($value));
173175
}
174176

175-
return $context;
177+
return $partialContext;
176178
}
177179

178180
protected function allowDynamicPartials(): bool

tests/Integration/Tags/RenderTagTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
);
2323
});
2424

25+
test('render passes parent variable as named arguments into inner scope', function () {
26+
assertTemplateResult(
27+
'My Product',
28+
'{% render "product", product: a %}',
29+
data: ['a' => ['title' => 'My Product']],
30+
partials: ['product' => '{{ product.title }}'],
31+
);
32+
});
33+
2534
test('render accepts literals as arguments', function () {
2635
assertTemplateResult(
2736
'123',

0 commit comments

Comments
 (0)