Skip to content

[BUGFIX] Ignore f:then/f:else outside condition context #1052

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions src/Core/ViewHelper/AbstractConditionViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected function renderThenChild()
foreach ($this->viewHelperNode->getChildNodes() as $childNode) {
if ($childNode instanceof ViewHelperNode
&& str_ends_with($childNode->getViewHelperClassName(), 'ThenViewHelper')) {
$data = $childNode->evaluate($this->renderingContext);
$data = $childNode->evaluateChildNodes($this->renderingContext);
return $data;
}
if ($childNode instanceof ViewHelperNode
Expand Down Expand Up @@ -184,7 +184,7 @@ protected function renderElseChild()
$arguments = $childNode->getArguments();
if (isset($arguments['if'])) {
if ($arguments['if']->evaluate($this->renderingContext)) {
return $childNode->evaluate($this->renderingContext);
return $childNode->evaluateChildNodes($this->renderingContext);
}
} elseif ($elseNode === null) {
$elseNode = $childNode;
Expand All @@ -205,7 +205,7 @@ protected function renderElseChild()

// If a f:else node exists, evaluate its content
if ($elseNode instanceof ViewHelperNode) {
return $elseNode->evaluate($this->renderingContext);
return $elseNode->evaluateChildNodes($this->renderingContext);
}

// If only the condition is specified, but no then/else handling, the whole ViewHelper should
Expand Down
12 changes: 7 additions & 5 deletions src/ViewHelpers/ElseViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ public function initializeArguments()
}

/**
* @return string the rendered string
* Does nothing unless used in context of condition ViewHelper, such as f:if
*
* @api
* @todo consider throwing an exception here in future versions
*/
public function render()
public function render(): string
{
return $this->renderChildren();
return '';
}

/**
Expand All @@ -64,9 +66,9 @@ public function render()
* @param string $initializationPhpCode
* @param ViewHelperNode $node
* @param TemplateCompiler $compiler
* @return string|null
* @return string
*/
public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler): string
{
return '\'\'';
}
Expand Down
10 changes: 5 additions & 5 deletions src/ViewHelpers/ThenViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class ThenViewHelper extends AbstractViewHelper
protected $escapeOutput = false;

/**
* Just render everything.
* Does nothing unless used in context of condition ViewHelper, such as f:if
*
* @return string the rendered string
* @api
* @todo consider throwing an exception here in future versions
*/
public function render()
public function render(): string
{
return $this->renderChildren();
return '';
}

/**
Expand All @@ -43,7 +43,7 @@ public function render()
* @param TemplateCompiler $compiler
* @return string
*/
public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler): string
{
return '\'\'';
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Functional/ViewHelpers/IfThenElseViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,27 @@ public static function renderDataProvider(): \Generator
true,
];

yield 'f:then without f:if context' => [
'<f:then>foo</f:then>',
[],
'',
];
yield 'f:else without f:if context' => [
'<f:else>foo</f:else>',
[],
'',
];
yield 'f:else-if without f:if context, verdict true' => [
'<f:else if="{verdict}">foo</f:else>',
['verdict' => true],
'',
];
yield 'f:else-if without f:if context, verdict false' => [
'<f:else if="{verdict}">foo</f:else>',
['verdict' => false],
'',
];

/*
* @todo This should work but doesn't at the moment. This is probably related to the boolean
* parser not converting variable nodes correctly. There is a related todo in the BooleanParserTest.
Expand Down