Created
May 9, 2017 06:07
-
-
Save akiessling/e10b1de253a3bbdf4d90e3bdef4d7144 to your computer and use it in GitHub Desktop.
Array Chunk ViewHelper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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