2 namespace TYPO3\CMS\Fluid\ViewHelpers
;
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
11 * The TYPO3 project - inspiring people to share! *
14 use TYPO3\CMS\Core\Utility\GeneralUtility
;
15 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
;
16 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception
;
17 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface
;
18 use TYPO3Fluid\Fluid\ViewHelpers\SwitchViewHelper
as OriginalSwitchViewHelper
;
21 * Case view helper that is only usable within the SwitchViewHelper.
22 * @see \TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper
26 class CaseViewHelper
extends AbstractViewHelper
31 protected $escapeOutput = false;
34 * Initialize ViewHelper arguments
38 public function initializeArguments()
40 $this->registerArgument('value', 'mixed', 'The switch value. If it matches, the child will be rendered.', false, null);
41 $this->registerArgument('default', 'bool', 'If this is set, this child will be rendered, if none else matches.', false, false);
45 * @return string the contents of this view helper if $value equals the expression of the surrounding switch view helper, or $default is TRUE. otherwise an empty string
50 public function render()
52 return static::renderStatic(
54 'value' => $this->arguments
['value'],
55 'default' => $this->arguments
['default']
57 $this->buildRenderChildrenClosure(),
58 $this->renderingContext
63 * @param array $arguments
64 * @param \Closure $renderChildrenClosure
65 * @param RenderingContextInterface $renderingContext
67 * @return mixed|string
70 public static function renderStatic(array $arguments, \Closure
$renderChildrenClosure, RenderingContextInterface
$renderingContext)
72 $value = $arguments['value'];
73 $default = (bool)$arguments['default'];
74 $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
75 if ($default !== false) {
76 GeneralUtility
::deprecationLog('Argument "default" on f:case is deprecated - use f:defaultCase instead');
78 if ($value === null && $default === false) {
79 throw new Exception('The case View helper must have either value or default argument', 1382867521);
81 $expression = $viewHelperVariableContainer->get(OriginalSwitchViewHelper
::class, 'switchExpression');
83 // non-type-safe comparison by intention
84 if ($default === true ||
$expression == $value) {
85 $viewHelperVariableContainer->addOrUpdate(OriginalSwitchViewHelper
::class, 'break', true);
86 return $renderChildrenClosure();