Skip to content

Instantly share code, notes, and snippets.

@rpflamm
Last active February 9, 2016 09:56
Show Gist options
  • Save rpflamm/fb14e57e7effb20cc62f to your computer and use it in GitHub Desktop.
Save rpflamm/fb14e57e7effb20cc62f to your computer and use it in GitHub Desktop.
ShuffleViewHelper
{namespace ve=Vendor\Extension\ViewHelpers}
<f:for each="{yourArray -> ve:shuffle(limit:3)}" as="item">
{item.name}
</f:for>
<?php
namespace Vendor\Extension\ViewHelpers;
class ShuffleViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
* @param mixed $subject array to randomize.
* @param int $limit If > 0 then array is sliced
*
* @return array shuffled and sliced array
*/
public function render($subject = NULL, $limit = 0) {
if ($subject === NULL) {
$subject = $this->renderChildren();
}
if ( is_array($subject) ) shuffle($subject);
if ( is_array($subject) && $limit > 0 ) $subject = array_slice($subject, 0, $limit)
return $subject;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment