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

[BUGFIX] Properly restore renderingContext in renderChildren() #970

Merged
merged 1 commit into from
Aug 20, 2024
Merged
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
38 changes: 22 additions & 16 deletions src/Core/ViewHelper/AbstractViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ abstract class AbstractViewHelper implements ViewHelperInterface
*/
protected $renderingContext;

/**
* Stores rendering contexts in a situation where ViewHelpers are called recursively from inside
* one of their child nodes. In that case, the rendering context can change during the recursion,
* but needs to be restored properly after each run. Thus, we store a stack of rendering contexts
* to be able to restore the initial state of the ViewHelper.
*
* @var RenderingContextInterface[]
*/
protected array $renderingContextStack = [];

/**
* @var \Closure
*/
Expand Down Expand Up @@ -308,23 +318,19 @@ public function renderChildren()
*/
protected function buildRenderChildrenClosure()
{
$argumentName = $this->getContentArgumentName();
$arguments = $this->arguments;
if ($argumentName !== null && isset($arguments[$argumentName])) {
$renderChildrenClosure = function () use ($arguments, $argumentName) {
return $arguments[$argumentName];
};
} else {
$self = clone $this;
$renderChildrenClosure = function () use ($self) {
if ($self->renderChildrenClosure !== null) {
$closure = $self->renderChildrenClosure;
return $closure();
}
return $self->viewHelperNode->evaluateChildNodes($self->renderingContext);
};
$contentArgumentName = $this->getContentArgumentName();
if ($contentArgumentName !== null && isset($this->arguments[$contentArgumentName])) {
return fn() => $this->arguments[$contentArgumentName];
}
if ($this->renderChildrenClosure !== null) {
return $this->renderChildrenClosure;
}
return $renderChildrenClosure;
return function () {
$this->renderingContextStack[] = $this->renderingContext;
$result = $this->viewHelperNode->evaluateChildNodes($this->renderingContext);
$this->setRenderingContext(array_pop($this->renderingContextStack));
return $result;
};
}

/**
Expand Down