From: Fabien Potencier Date: Thu, 21 Feb 2013 06:38:58 +0000 (+0100) Subject: added a batch filter X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=a50bc8c95be668c35cda5a6efc6a1e8413d52670;p=web%2Fkonrad%2Ftwig.git added a batch filter --- diff --git a/CHANGELOG b/CHANGELOG index 087f470..5cd3a65 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ * 1.12.3 (2013-XX-XX) - * n/a + * added a batch filter * 1.12.2 (2013-02-09) diff --git a/doc/filters/batch.test b/doc/filters/batch.test new file mode 100644 index 0000000..4366b57 --- /dev/null +++ b/doc/filters/batch.test @@ -0,0 +1,45 @@ +``batch`` +========= + +.. versionadded:: 1.12.3 + The batch filter was added in Twig 1.12.3. + +The ``batch`` filter "batches" items by returning a list of lists with the +given number of items. If you provide a second parameter, it is used to fill +missing items: + +.. code-block:: jinja + + {% set items = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] %} + + + {% for row in items|batch(3, 'No item') %} + + {% for column in row %} + + {% endfor %} + + {% endfor %} +
{{ column }}
+ +The above example will be rendered as: + +.. code-block:: jinja + + + + + + + + + + + + + + + + + +
abc
def
gNo itemNo item
diff --git a/doc/filters/index.rst b/doc/filters/index.rst index 888693e..39b1d27 100644 --- a/doc/filters/index.rst +++ b/doc/filters/index.rst @@ -33,3 +33,4 @@ Filters first last trim + batch diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 6a2e142..a856b59 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -152,6 +152,7 @@ class Twig_Extension_Core extends Twig_Extension new Twig_SimpleFilter('split', 'twig_split_filter'), new Twig_SimpleFilter('sort', 'twig_sort_filter'), new Twig_SimpleFilter('merge', 'twig_array_merge'), + new Twig_SimpleFilter('batch', 'twig_array_batch'), // string/array filters new Twig_SimpleFilter('reverse', 'twig_reverse_filter', array('needs_environment' => true)), @@ -1307,3 +1308,30 @@ function twig_constant($constant, $object = null) return constant($constant); } + +/** + * Batches item. + * + * @param array $items An array of items + * @param integer $size The size of the batch + * @param string $fill A string to fill missing items + * + * @return array + */ +function twig_array_batch($items, $size, $fill = null) +{ + if ($items instanceof Traversable) { + $items = iterator_to_array($items, false); + } + + $result = array_chunk($items, $size, true); + + if (null !== $fill) { + $last = count($result) - 1; + while (count($result[$last]) < $size) { + $result[$last][] = $fill; + } + } + + return $result; +} diff --git a/test/Twig/Tests/Fixtures/filters/batch.test b/test/Twig/Tests/Fixtures/filters/batch.test new file mode 100644 index 0000000..af996f2 --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/batch.test @@ -0,0 +1,37 @@ +--TEST-- +"batch" filter +--TEMPLATE-- + +{% for row in items|batch(3, '') %} + + {% for column in row %} + + {% endfor %} + +{% endfor %} +
{{ column }}
+--DATA-- +return array('items' => array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')) +--EXPECT-- + + + + + + + + + + + + + + + + + + + + + +
abc
def
ghi
j