TYPO3 CMS  TYPO3_6-2
ForViewHelper.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 
62 
74  public function render($each, $as, $key = '', $reverse = FALSE, $iteration = NULL) {
75  return self::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
76  }
77 
85  static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext) {
86  $templateVariableContainer = $renderingContext->getTemplateVariableContainer();
87  if ($arguments['each'] === NULL) {
88  return '';
89  }
90  if (is_object($arguments['each']) && !$arguments['each'] instanceof \Traversable) {
91  throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('ForViewHelper only supports arrays and objects implementing \Traversable interface', 1248728393);
92  }
93 
94  if ($arguments['reverse'] === TRUE) {
95  // array_reverse only supports arrays
96  if (is_object($arguments['each'])) {
97  $arguments['each'] = iterator_to_array($arguments['each']);
98  }
99  $arguments['each'] = array_reverse($arguments['each']);
100  }
101  if ($arguments['iteration'] !== NULL) {
102  $iterationData = array(
103  'index' => 0,
104  'cycle' => 1,
105  'total' => count($arguments['each'])
106  );
107  }
108 
109  $output = '';
110  foreach ($arguments['each'] as $keyValue => $singleElement) {
111  $templateVariableContainer->add($arguments['as'], $singleElement);
112  if ($arguments['key'] !== '') {
113  $templateVariableContainer->add($arguments['key'], $keyValue);
114  }
115  if ($arguments['iteration'] !== NULL) {
116  $iterationData['isFirst'] = $iterationData['cycle'] === 1;
117  $iterationData['isLast'] = $iterationData['cycle'] === $iterationData['total'];
118  $iterationData['isEven'] = $iterationData['cycle'] % 2 === 0;
119  $iterationData['isOdd'] = !$iterationData['isEven'];
120  $templateVariableContainer->add($arguments['iteration'], $iterationData);
121  $iterationData['index']++;
122  $iterationData['cycle']++;
123  }
124  $output .= $renderChildrenClosure();
125  $templateVariableContainer->remove($arguments['as']);
126  if ($arguments['key'] !== '') {
127  $templateVariableContainer->remove($arguments['key']);
128  }
129  if ($arguments['iteration'] !== NULL) {
130  $templateVariableContainer->remove($arguments['iteration']);
131  }
132  }
133  return $output;
134  }
135 }
static renderStatic(array $arguments, \Closure $renderChildrenClosure, \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext)
render($each, $as, $key='', $reverse=FALSE, $iteration=NULL)