Skip to content

Commit edabe97

Browse files
committed
[BUGFIX] Ignore f:then/f:else outside condition context
Before this change, both `f:then` and `f:else` could be used outside of a condition tag and would then always output the content, regardless of any condition. Now, nothing is outputted. In the future, we might consider throwing an exception here to alert the user about invalid Fluid syntax. However, this would be a breaking change. Resolves: #1020
1 parent 67d3e9d commit edabe97

4 files changed

Lines changed: 35 additions & 12 deletions

File tree

src/Core/ViewHelper/AbstractConditionViewHelper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ protected function renderThenChild()
118118
foreach ($this->viewHelperNode->getChildNodes() as $childNode) {
119119
if ($childNode instanceof ViewHelperNode
120120
&& str_ends_with($childNode->getViewHelperClassName(), 'ThenViewHelper')) {
121-
$data = $childNode->evaluate($this->renderingContext);
121+
$data = $childNode->evaluateChildNodes($this->renderingContext);
122122
return $data;
123123
}
124124
if ($childNode instanceof ViewHelperNode
@@ -184,7 +184,7 @@ protected function renderElseChild()
184184
$arguments = $childNode->getArguments();
185185
if (isset($arguments['if'])) {
186186
if ($arguments['if']->evaluate($this->renderingContext)) {
187-
return $childNode->evaluate($this->renderingContext);
187+
return $childNode->evaluateChildNodes($this->renderingContext);
188188
}
189189
} elseif ($elseNode === null) {
190190
$elseNode = $childNode;
@@ -205,7 +205,7 @@ protected function renderElseChild()
205205

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

211211
// If only the condition is specified, but no then/else handling, the whole ViewHelper should

src/ViewHelpers/ElseViewHelper.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ public function initializeArguments()
5050
}
5151

5252
/**
53-
* @return string the rendered string
53+
* Does nothing unless used in context of condition ViewHelper, such as f:if
54+
*
5455
* @api
56+
* @todo consider throwing an exception here in future versions
5557
*/
56-
public function render()
58+
public function render(): string
5759
{
58-
return $this->renderChildren();
60+
return '';
5961
}
6062

6163
/**
@@ -66,7 +68,7 @@ public function render()
6668
* @param TemplateCompiler $compiler
6769
* @return string|null
6870
*/
69-
public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
71+
public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler): string
7072
{
7173
return '\'\'';
7274
}

src/ViewHelpers/ThenViewHelper.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ class ThenViewHelper extends AbstractViewHelper
2525
protected $escapeOutput = false;
2626

2727
/**
28-
* Just render everything.
28+
* Does nothing unless used in context of condition ViewHelper, such as f:if
2929
*
30-
* @return string the rendered string
3130
* @api
31+
* @todo consider throwing an exception here in future versions
3232
*/
33-
public function render()
33+
public function render(): string
3434
{
35-
return $this->renderChildren();
35+
return '';
3636
}
3737

3838
/**
@@ -43,7 +43,7 @@ public function render()
4343
* @param TemplateCompiler $compiler
4444
* @return string
4545
*/
46-
public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
46+
public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler): string
4747
{
4848
return '\'\'';
4949
}

tests/Functional/ViewHelpers/IfThenElseViewHelperTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,27 @@ public static function renderDataProvider(): \Generator
566566
true,
567567
];
568568

569+
yield 'f:then without f:if context' => [
570+
'<f:then>foo</f:then>',
571+
[],
572+
'',
573+
];
574+
yield 'f:else without f:if context' => [
575+
'<f:else>foo</f:else>',
576+
[],
577+
'',
578+
];
579+
yield 'f:else-if without f:if context, verdict true' => [
580+
'<f:else if="{verdict}">foo</f:else>',
581+
['verdict' => true],
582+
'',
583+
];
584+
yield 'f:else-if without f:if context, verdict false' => [
585+
'<f:else if="{verdict}">foo</f:else>',
586+
['verdict' => false],
587+
'',
588+
];
589+
569590
/*
570591
* @todo This should work but doesn't at the moment. This is probably related to the boolean
571592
* parser not converting variable nodes correctly. There is a related todo in the BooleanParserTest.

0 commit comments

Comments
 (0)