* 0.9.6-DEV
+ * added the cycle filter
* fixed the Lexer when mbstring.func_overload is used with an mbstring.internal_encoding different from ASCII
* added a long-syntax for the set tag ({% set foo %}...{% endset %})
* unit tests are now powered by PHPUnit
[twig]
{{ var|odd ? 'odd' : 'even' }}
+### `cycle`
+
+The `cycle` filter can be used to cycle between an array of values:
+
+ [twig]
+ {% for i in 0..10 %}
+ {{ ['odd', 'even']|cycle(i) }}
+ {% endfor %}
+
+The array can contain any number of values:
+
+ [twig]
+ {% set fruits as ['apple', 'orange', 'citrus'] %}
+
+ {% for i in 0..10 %}
+ {{ fruits|cycle(i) }}
+ {% endfor %}
+
### `encoding`
The `encoding` filter URL encode a given string.
'sort' => new Twig_Filter_Function('twig_sort_filter'),
'in' => new Twig_Filter_Function('twig_in_filter'),
'range' => new Twig_Filter_Function('twig_range_filter'),
+ 'cycle' => new Twig_Filter_Function('twig_cycle_filter'),
// iteration and runtime
'default' => new Twig_Filter_Function('twig_default_filter'),
return range($start, $end, $step);
}
+function twig_cycle_filter($values, $i)
+{
+ if (!is_array($values) && !$values instanceof ArrayAccess)
+ {
+ return $values;
+ }
+
+ return $values[$i % count($values)];
+}
+
/*
* Each type specifies a way for applying a transformation to a string
* The purpose is for the string to be "escaped" so it is suitable for
--- /dev/null
+--TEST--
+"cycle" filter
+--TEMPLATE--
+{% for i in 0..6 %}
+{{ array1|cycle(i) }}-{{ array2|cycle(i) }}
+{% endfor %}
+--DATA--
+return array('array1' => array('odd', 'even'), 'array2' => array('apple', 'orange', 'citrus'))
+--EXPECT--
+odd-apple
+even-orange
+odd-citrus
+even-apple
+odd-orange
+even-citrus
+odd-apple