Permalink
Browse files

optimize batch function and add tests

  • Loading branch information...
1 parent 15336c1 commit ccf3cd1c81711849c311d0d66b402ef2592c60a3 @felds felds committed Feb 21, 2013
@@ -1328,9 +1328,10 @@ function twig_array_batch($items, $size, $fill = null)
if (null !== $fill) {
$last = count($result) - 1;
- while (count($result[$last]) < $size) {
- $result[$last][] = $fill;
- }
+ $result[$last] = array_merge(
+ $result[$last],
+ array_fill(0, $size - count($result[$last]), $fill)
+ );
}
return $result;
@@ -1,37 +1,31 @@
--TEST--
"batch" filter
--TEMPLATE--
-<table>
-{% for row in items|batch(3, '') %}
- <tr>
+{% for row in items|batch(3) %}
+ <div class=row>
{% for column in row %}
- <td>{{ column }}</td>
+ <div class=item>{{ column }}</div>
{% endfor %}
- </tr>
+ </div>
{% endfor %}
-</table>
--DATA--
return array('items' => array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'))
--EXPECT--
-<table>
- <tr>
- <td>a</td>
- <td>b</td>
- <td>c</td>
- </tr>
- <tr>
- <td>d</td>
- <td>e</td>
- <td>f</td>
- </tr>
- <tr>
- <td>g</td>
- <td>h</td>
- <td>i</td>
- </tr>
- <tr>
- <td>j</td>
- <td></td>
- <td></td>
- </tr>
-</table>
+<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>
@@ -0,0 +1,37 @@
+--TEST--
+"batch" filter
+--TEMPLATE--
+<table>
+{% for row in items|batch(3, '') %}
+ <tr>
+ {% for column in row %}
+ <td>{{ column }}</td>
+ {% endfor %}
+ </tr>
+{% endfor %}
+</table>
+--DATA--
+return array('items' => array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'))
+--EXPECT--
+<table>
+ <tr>
+ <td>a</td>
+ <td>b</td>
+ <td>c</td>
+ </tr>
+ <tr>
+ <td>d</td>
+ <td>e</td>
+ <td>f</td>
+ </tr>
+ <tr>
+ <td>g</td>
+ <td>h</td>
+ <td>i</td>
+ </tr>
+ <tr>
+ <td>j</td>
+ <td></td>
+ <td></td>
+ </tr>
+</table>
@@ -0,0 +1,37 @@
+--TEST--
+"batch" filter
+--TEMPLATE--
+<table>
+{% for row in items|batch(3, 'fill') %}
+ <tr>
+ {% for column in row %}
+ <td>{{ column }}</td>
+ {% endfor %}
+ </tr>
+{% endfor %}
+</table>
+--DATA--
+return array('items' => array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'))
+--EXPECT--
+<table>
+ <tr>
+ <td>a</td>
+ <td>b</td>
+ <td>c</td>
+ </tr>
+ <tr>
+ <td>d</td>
+ <td>e</td>
+ <td>f</td>
+ </tr>
+ <tr>
+ <td>g</td>
+ <td>h</td>
+ <td>i</td>
+ </tr>
+ <tr>
+ <td>j</td>
+ <td>fill</td>
+ <td>fill</td>
+ </tr>
+</table>

0 comments on commit ccf3cd1

Please sign in to comment.