Skip to content

Instantly share code, notes, and snippets.

@akiessling
Created May 9, 2017 06:07
Show Gist options
  • Select an option

  • Save akiessling/e10b1de253a3bbdf4d90e3bdef4d7144 to your computer and use it in GitHub Desktop.

Select an option

Save akiessling/e10b1de253a3bbdf4d90e3bdef4d7144 to your computer and use it in GitHub Desktop.
Array Chunk ViewHelper
<?php
namespace AndreasKießling\AkViewHelpers\ViewHelpers;
/**
* This class is a view helper for fluid to call array_chunk from a template
*/
class ArrayChunkViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
{
/**
* @param integer $size
* @param mixed $array
* @param string $as
* @return string
* @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
*/
public function render($size, $array = null, $as = 'chunkedArray')
{
if ($array === null) {
return '';
}
if (is_object($array)) {
if (!$array instanceof \Traversable) {
throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('ArrayChunkViewHelper only supports arrays and objects implementing Traversable interface' , 1340274893);
}
$array = iterator_to_array($array);
}
$chunks = array_chunk($array, $size);
$this->templateVariableContainer->add($as, $chunks);
$output = $this->renderChildren();
$this->templateVariableContainer->remove($as);
return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment