*
* @param array $items An array of items
* @param integer $size The size of the batch
- * @param string $fill A string to fill missing items
+ * @param mixed $fill A value used to fill missing items
*
* @return array
*/
if (null !== $fill) {
$last = count($result) - 1;
- $result[$last] = array_merge(
- $result[$last],
- array_fill(0, $size - count($result[$last]), $fill)
- );
+ if ($fillCount = $size - count($result[$last])) {
+ $result[$last] = array_merge(
+ $result[$last],
+ array_fill(0, $fillCount, $fill)
+ );
+ }
}
return $result;
--- /dev/null
+--TEST--
+"batch" filter
+--TEMPLATE--
+{% for row in items|batch(3, 'fill') %}
+ <div class=row>
+ {% for column in row %}
+ <div class=item>{{ column }}</div>
+ {% endfor %}
+ </div>
+{% endfor %}
+--DATA--
+return array('items' => array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'))
+--EXPECT--
+<div class=row>
+ <div class=item>a</div>
+ <div class=item>b</div>
+ <div class=item>c</div>
+ </div>
+ <div class=row>
+ <div class=item>d</div>
+ <div class=item>e</div>
+ <div class=item>f</div>
+ </div>
+ <div class=row>
+ <div class=item>g</div>
+ <div class=item>h</div>
+ <div class=item>i</div>
+ </div>
+ <div class=row>
+ <div class=item>j</div>
+ <div class=item>k</div>
+ <div class=item>l</div>
+ </div>