Skip to content

Instantly share code, notes, and snippets.

@stevelacey
Created August 2, 2012 09:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevelacey/3235720 to your computer and use it in GitHub Desktop.
Save stevelacey/3235720 to your computer and use it in GitHub Desktop.
Twig Chunk Extension
<?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';
}
}
services:
acme.twig.chunk_extension:
class: Acme\DemoBundle\Twig\ChunkExtension
tags:
- { name: twig.extension }
@ureimers
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment