/** * Iterates through elements of $each and renders child nodes * * @param array $each The array or Tx_Extbase_Persistence_ObjectStorage to iterated over * @param string $as The name of the iteration variable * @param string $key The name of the variable to store the current array key * @param boolean $reverse If enabled, the iterator will start with the last element and proceed reversely * @param string $iteration The name of the variable to store iteration information (index, cycle, isFirst, isLast, isEven, isOdd) * @param int $required The amount of required loops. * @return string Rendered string * @author Sebastian Kurfürst <*****@*****.**> * @author Bastian Waidelich <*****@*****.**> * @author Robert Lemke <*****@*****.**> * @api */ public function render($each, $as, $key = '', $reverse = FALSE, $iteration = NULL, $required = 0) { if (is_object($each) && $each instanceof Traversable) { $each = iterator_to_array($each); } if ($required > 0) { $count = count($each); for ($i = 0; $i < $required - $count; $i++) { $each[] = null; } } return parent::render($each, $as, $key, $reverse, $iteration); }
/** * @test * @author Bastian Waidelich <*****@*****.**> */ public function iterationDataIsAddedToTemplateVariableContainerIfIterationArgumentIsSet() { $viewHelper = new Tx_Fluid_ViewHelpers_ForViewHelper(); $mockViewHelperNode = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_ViewHelperNode', array('evaluateChildNodes'), array(), '', FALSE); $mockViewHelperNode->expects($this->any())->method('evaluateChildNodes')->will($this->returnValue('foo')); $this->templateVariableContainer->expects($this->at(0))->method('add')->with('innerVariable', 'bar'); $this->templateVariableContainer->expects($this->at(1))->method('add')->with('iteration', array('index' => 0, 'cycle' => 1, 'total' => 3, 'isFirst' => TRUE, 'isLast' => FALSE, 'isEven' => FALSE, 'isOdd' => TRUE)); $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('innerVariable'); $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('iteration'); $this->templateVariableContainer->expects($this->at(4))->method('add')->with('innerVariable', 'Fluid'); $this->templateVariableContainer->expects($this->at(5))->method('add')->with('iteration', array('index' => 1, 'cycle' => 2, 'total' => 3, 'isFirst' => FALSE, 'isLast' => FALSE, 'isEven' => TRUE, 'isOdd' => FALSE)); $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('innerVariable'); $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('iteration'); $this->templateVariableContainer->expects($this->at(8))->method('add')->with('innerVariable', 'rocks'); $this->templateVariableContainer->expects($this->at(9))->method('add')->with('iteration', array('index' => 2, 'cycle' => 3, 'total' => 3, 'isFirst' => FALSE, 'isLast' => TRUE, 'isEven' => FALSE, 'isOdd' => TRUE)); $this->templateVariableContainer->expects($this->at(10))->method('remove')->with('innerVariable'); $this->templateVariableContainer->expects($this->at(11))->method('remove')->with('iteration'); $viewHelper->setTemplateVariableContainer($this->templateVariableContainer); $viewHelper->setRenderingContext($this->renderingContext); $viewHelper->setViewHelperNode($mockViewHelperNode); $viewHelper->render(array('foo' => 'bar', 'FLOW3' => 'Fluid', 'TYPO3' => 'rocks'), 'innerVariable', '', FALSE, 'iteration'); }
/** * @test * @author Bastian Waidelich <*****@*****.**> */ public function keyContainsTheNumericalIndexWhenIteratingThroughElementsOfObjectsOfTyeSplObjectStorage() { $viewHelper = new Tx_Fluid_ViewHelpers_ForViewHelper(); $variableContainer = new Tx_Fluid_Core_ViewHelper_TemplateVariableContainer(array()); $viewHelperNode = new Tx_Fluid_ViewHelpers_Fixtures_ConstraintSyntaxTreeNode($variableContainer); $viewHelper->setTemplateVariableContainer($variableContainer); $viewHelper->setViewHelperNode($viewHelperNode); $splObjectStorageObject = new SplObjectStorage(); $object1 = new stdClass(); $splObjectStorageObject->attach($object1); $object2 = new stdClass(); $splObjectStorageObject->attach($object2); $object3 = new stdClass(); $splObjectStorageObject->attach($object3); $viewHelper->render($splObjectStorageObject, 'innerVariable', 'someKey'); $expectedCallProtocol = array(array('innerVariable' => $object1, 'someKey' => 0), array('innerVariable' => $object2, 'someKey' => 1), array('innerVariable' => $object3, 'someKey' => 2)); $this->assertSame($expectedCallProtocol, $viewHelperNode->callProtocol, 'The call protocol differs -> The for loop does not work as it should!'); }