[BUGFIX] Properly restore renderingContext in renderChildren() (#970) #980
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It has long been possible to render recursive templates with Fluid, either by using partials or sections. In both cases,
<f:render />
is used to initiate the recursive behavior.For example, the following template creates a recursive main menu by using sections:
However, this was only working because
ForViewHelper
was implemented as static ViewHelper by using Fluid's traitCompileWithRenderStatic
. The trait changed Fluid's handling of ViewHelpers in a way that variables stay consistent even during recursions. This was made possible back in Fluid 1.0.5 with 819ca24.However, this never worked properly with ViewHelpers not using
renderStatic()
but rather the defaultrender()
method. In those implementations,$this->renderChildren()
is used to access and render the ViewHelper's child nodes rather than executing the closure directly passed torenderStatic()
.To support recursions also for ViewHelpers not using
renderStatic()
, this patch introduces new state toAbstractViewHelper
to keep track of rendering contexts during recursions. Note that this state is only used for uncached templates, for cached templates this already worked without any changes.To explain the new behavior, let's take the example above and let's also assume that
ForViewHelper
is implemented usingrender()
rather thanrenderStatic()
. In uncached templates, the new behavior works as follows:$view->render()
gets called for the templateTemplateParser
creates nodes from template string, among them oneViewHelperNode
for each ViewHelper.RenderViewHelper
gets called by executing$viewHelperNode->execute()
, which in turn callsViewHelperInvoker
, which in the end calls the render method of the ViewHelper (in this caseRenderViewHelper::renderStatic()
)RenderViewHelper
calls$view->renderSection()
, which clones the current rendering context and puts the appropriate variables in it. The old rendering context gets saved to be restored later for the parent template.ForViewHelper
gets called, we end up inForViewHelper::render()
(because we use our custom implementation withoutrenderStatic()
)$this->renderChildren()
is called for each{item}
.{item.children}
is defined, things get interesting.RenderViewHelper
calls$view->renderSection()
again, which creates and saves another new rendering context, see 4. We're in a recursion now.So the template/partial/section gets restored properly, but the
ForViewHelper
still has the last rendering context from within the recursion because Fluid didn't restore it to the previous state. In fact, Fluid couldn't really do that from outside because$forViewHelper->renderChildren()
initiated the whole recursion and is still running (once for each recursion level).To solve this dilemma, this patch implements a rendering context stack similarly to the one used in the
$view
object, but on the ViewHelper level. Each ViewHelper keeps track of recursive rendering contexts for itself – again, only for uncached, non-static ViewHelpers.In addition to that, the patch simplifies the logic of
buildRenderChildrenClosure()
, which covers both cached/uncached and the contentArgumentName feature.