7637299da05e6cd6b3275bedfd4cd11ad336ba49
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / SwitchViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
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. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13 use TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler;
14 use TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode;
15 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
16 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
17 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\ChildNodeAccessInterface;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
19
20 /**
21 * Switch view helper which can be used to render content depending on a value or expression.
22 * Implements what a basic switch()-PHP-method does.
23 *
24 * = Examples =
25 *
26 * <code title="Simple Switch statement">
27 * <f:switch expression="{person.gender}">
28 * <f:case value="male">Mr.</f:case>
29 * <f:case value="female">Mrs.</f:case>
30 * <f:case default="TRUE">Mrs. or Mr.</f:case>
31 * </f:switch>
32 * </code>
33 * <output>
34 * Mr. / Mrs. (depending on the value of {person.gender}) or if no value evaluates to TRUE, default case
35 * </output>
36 *
37 * Note: Using this view helper can be a sign of weak architecture. If you end up using it extensively
38 * you might want to consider restructuring your controllers/actions and/or use partials and sections.
39 * E.g. the above example could be achieved with <f:render partial="title.{person.gender}" /> and the partials
40 * "title.male.html", "title.female.html", ...
41 * Depending on the scenario this can be easier to extend and possibly contains less duplication.
42 *
43 * @api
44 */
45 class SwitchViewHelper extends AbstractViewHelper implements ChildNodeAccessInterface, CompilableInterface {
46
47 /**
48 * An array of \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode
49 * @var array
50 */
51 private $childNodes = array();
52
53 /**
54 * @var mixed
55 */
56 protected $backupSwitchExpression = NULL;
57
58 /**
59 * @var bool
60 */
61 protected $backupBreakState = FALSE;
62
63 /**
64 * Setter for ChildNodes - as defined in ChildNodeAccessInterface
65 *
66 * @param array $childNodes Child nodes of this syntax tree node
67 * @return void
68 */
69 public function setChildNodes(array $childNodes) {
70 $this->childNodes = $childNodes;
71 }
72
73 /**
74 * @param mixed $expression
75 * @return string the rendered string
76 * @api
77 */
78 public function render($expression) {
79 return self::renderStatic(
80 array(
81 'expression' => $expression
82 ),
83 $this->buildRenderChildrenClosure(),
84 $this->renderingContext
85 );
86 }
87
88 /**
89 * Default implementation for CompilableInterface. See CompilableInterface
90 * for a detailed description of this method.
91 *
92 * @param array $arguments
93 * @param \Closure $renderChildrenClosure
94 * @param RenderingContextInterface $renderingContext
95 * @return mixed
96 * @see \TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface
97 */
98 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
99 $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
100
101 $stackValue = array(
102 'expression' => $arguments['expression'],
103 'break' => FALSE
104 );
105
106 if ($viewHelperVariableContainer->exists(SwitchViewHelper::class, 'stateStack')) {
107 $stateStack = $viewHelperVariableContainer->get(SwitchViewHelper::class, 'stateStack');
108 } else {
109 $stateStack = array();
110 }
111 $stateStack[] = $stackValue;
112 $viewHelperVariableContainer->addOrUpdate(SwitchViewHelper::class, 'stateStack', $stateStack);
113
114 $result = $renderChildrenClosure();
115
116 $stateStack = $viewHelperVariableContainer->get(SwitchViewHelper::class, 'stateStack');
117 array_pop($stateStack);
118 $viewHelperVariableContainer->addOrUpdate(SwitchViewHelper::class, 'stateStack', $stateStack);
119
120 return $result;
121 }
122 }