Created
August 2, 2012 09:16
-
-
Save stevelacey/3235720 to your computer and use it in GitHub Desktop.
Twig Chunk Extension
This file contains 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 Acme\DemoBundle\Twig; | |
use Twig_Extension; | |
use Twig_Filter_Method; | |
class ChunkExtension extends \Twig_Extension | |
{ | |
public function getFilters() | |
{ | |
return array( | |
'chunk' => new Twig_Filter_Method($this, 'chunk'), | |
); | |
} | |
/** | |
* @param array $input | |
* @param int $size | |
* | |
* @return mixed | |
*/ | |
function chunk($input, $size) | |
{ | |
return array_chunk($input->toArray(), $size); | |
} | |
/** | |
* Returns the name of the extension. | |
* | |
* @return string The extension name | |
*/ | |
public function getName() | |
{ | |
return 'chunk'; | |
} | |
} |
This file contains 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
services: | |
acme.twig.chunk_extension: | |
class: Acme\DemoBundle\Twig\ChunkExtension | |
tags: | |
- { name: twig.extension } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you :)
Just found out that as of Twig 1.12.3 this can be done using the
batch
filter.http://twig.sensiolabs.org/doc/filters/batch.html