Last active
          February 9, 2016 09:56 
        
      - 
      
- 
        Save rpflamm/fb14e57e7effb20cc62f to your computer and use it in GitHub Desktop. 
    ShuffleViewHelper
  
        
  
    
      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
    
  
  
    
  | {namespace ve=Vendor\Extension\ViewHelpers} | |
| <f:for each="{yourArray -> ve:shuffle(limit:3)}" as="item"> | |
| {item.name} | |
| </f:for> | 
  
    
      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 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