TYPO3 CMS  TYPO3_6-2
AbstractConditionViewHelper.php
Go to the documentation of this file.
1 <?php
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 
35 
41  private $childNodes = array();
42 
49  public function setChildNodes(array $childNodes) {
50  $this->childNodes = $childNodes;
51  }
52 
56  public function __construct() {
57  $this->registerArgument('then', 'mixed', 'Value to be returned if the condition if met.', FALSE);
58  $this->registerArgument('else', 'mixed', 'Value to be returned if the condition if not met.', FALSE);
59  }
60 
69  protected function renderThenChild() {
70  if ($this->hasArgument('then')) {
71  return $this->arguments['then'];
72  }
73  if ($this->hasArgument('__thenClosure')) {
74  $thenClosure = $this->arguments['__thenClosure'];
75  return $thenClosure();
76  } elseif ($this->hasArgument('__elseClosure') || $this->hasArgument('else')) {
77  return '';
78  }
79 
80  $elseViewHelperEncountered = FALSE;
81  foreach ($this->childNodes as $childNode) {
82  if ($childNode instanceof \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode
83  && $childNode->getViewHelperClassName() === 'TYPO3\\CMS\\Fluid\\ViewHelpers\\ThenViewHelper') {
84  $data = $childNode->evaluate($this->renderingContext);
85  return $data;
86  }
87  if ($childNode instanceof \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode
88  && $childNode->getViewHelperClassName() === 'TYPO3\\CMS\\Fluid\\ViewHelpers\\ElseViewHelper') {
89  $elseViewHelperEncountered = TRUE;
90  }
91  }
92 
93  if ($elseViewHelperEncountered) {
94  return '';
95  } else {
96  return $this->renderChildren();
97  }
98  }
99 
108  protected function renderElseChild() {
109  if ($this->hasArgument('else')) {
110  return $this->arguments['else'];
111  }
112  if ($this->hasArgument('__elseClosure')) {
113  $elseClosure = $this->arguments['__elseClosure'];
114  return $elseClosure();
115  }
116  foreach ($this->childNodes as $childNode) {
117  if ($childNode instanceof \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode
118  && $childNode->getViewHelperClassName() === 'TYPO3\\CMS\\Fluid\\ViewHelpers\\ElseViewHelper') {
119  return $childNode->evaluate($this->renderingContext);
120  }
121  }
122 
123  return '';
124  }
125 
138  public function compile($argumentsVariableName, $renderChildrenClosureVariableName, &$initializationPhpCode, \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $syntaxTreeNode, \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler $templateCompiler) {
139  foreach ($syntaxTreeNode->getChildNodes() as $childNode) {
140  if ($childNode instanceof \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode
141  && $childNode->getViewHelperClassName() === 'TYPO3\\CMS\\Fluid\\ViewHelpers\\ThenViewHelper') {
142 
143  $childNodesAsClosure = $templateCompiler->wrapChildNodesInClosure($childNode);
144  $initializationPhpCode .= sprintf('%s[\'__thenClosure\'] = %s;', $argumentsVariableName, $childNodesAsClosure) . chr(10);
145  }
146  if ($childNode instanceof \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode
147  && $childNode->getViewHelperClassName() === 'TYPO3\\CMS\\Fluid\\ViewHelpers\\ElseViewHelper') {
148 
149  $childNodesAsClosure = $templateCompiler->wrapChildNodesInClosure($childNode);
150  $initializationPhpCode .= sprintf('%s[\'__elseClosure\'] = %s;', $argumentsVariableName, $childNodesAsClosure) . chr(10);
151  }
152  }
153  return \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler::SHOULD_GENERATE_VIEWHELPER_INVOCATION;
154  }
155 }
registerArgument($name, $type, $description, $required=FALSE, $defaultValue=NULL)
compile($argumentsVariableName, $renderChildrenClosureVariableName, &$initializationPhpCode, \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $syntaxTreeNode, \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler $templateCompiler)