From: Fabien Potencier Date: Mon, 20 Dec 2010 07:45:45 +0000 (+0100) Subject: converted the cycle filter to a function X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=b1b173f1676ab2cc98a218291afa0b3c04119d2f;p=web%2Fkonrad%2Ftwig.git converted the cycle filter to a function --- diff --git a/CHANGELOG b/CHANGELOG index 80b7381..f566917 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,7 @@ Backward incompatibilities: * the items filter, which has been deprecated for quite a long time now, has been removed * the range filter has been converted to a function: 0|range(10) -> range(0, 10) * the constant filter has been converted to a function: {{ some_date|date('DATE_W3C'|constant) }} -> {{ some_date|date(constant('DATE_W3C')) }} + * the cycle filter has been converted to a function: {{ ['odd', 'even']|cycle(i) }} -> {{ cycle(['odd', 'even'], i) }} Changes: diff --git a/doc/templates.rst b/doc/templates.rst index 07bbc09..c7d477f 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -1157,27 +1157,6 @@ The ``replace`` filter formats a given string by replacing the placeholders {# returns I like foo and bar. (if the foo parameter equals to the foo string) #} -``cycle`` -~~~~~~~~~ - -The ``cycle`` filter can be used to cycle between an array of values: - -.. code-block:: jinja - - {% for i in 0..10 %} - {{ ['odd', 'even']|cycle(i) }} - {% endfor %} - -The array can contain any number of values: - -.. code-block:: jinja - - {% set fruits = ['apple', 'orange', 'citrus'] %} - - {% for i in 0..10 %} - {{ fruits|cycle(i) }} - {% endfor %} - ``url_encode`` ~~~~~~~~~~~~~~ @@ -1447,6 +1426,27 @@ step of 1): {{ i }}, {% endfor %} +``cycle`` +~~~~~~~~~ + +The ``cycle`` function can be used to cycle on an array of values: + +.. code-block:: jinja + + {% for i in 0..10 %} + {{ cycle(['odd', 'even'], i) }} + {% endfor %} + +The array can contain any number of values: + +.. code-block:: jinja + + {% set fruits = ['apple', 'orange', 'citrus'] %} + + {% for i in 0..10 %} + {{ cycle(fruits, i) }} + {% endfor %} + ``constant`` ~~~~~~~~~~~~ diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index faa77cf..393e417 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -63,7 +63,6 @@ class Twig_Extension_Core extends Twig_Extension 'reverse' => new Twig_Filter_Function('twig_reverse_filter'), 'length' => new Twig_Filter_Function('twig_length_filter', array('needs_environment' => true)), 'sort' => new Twig_Filter_Function('twig_sort_filter'), - 'cycle' => new Twig_Filter_Function('twig_cycle_filter'), 'merge' => new Twig_Filter_Function('twig_array_merge'), // iteration and runtime @@ -91,8 +90,9 @@ class Twig_Extension_Core extends Twig_Extension public function getGlobals() { return array( - 'fn_range' => new Twig_Function($this, 'getRange'), + 'fn_range' => new Twig_Function($this, 'getRange'), 'fn_constant' => new Twig_Function($this, 'getConstant'), + 'fn_cycle' => new Twig_Function($this, 'getCycle'), ); } @@ -106,6 +106,15 @@ class Twig_Extension_Core extends Twig_Extension return constant($value); } + public function getCycle($values, $i) + { + if (!is_array($values) && !$values instanceof ArrayAccess) { + return $values; + } + + return $values[$i % count($values)]; + } + /** * Returns a list of filters to add to the existing list. * @@ -275,15 +284,6 @@ function twig_in_filter($value, $compare) return false; } -function twig_cycle_filter($values, $i) -{ - if (!is_array($values) && !$values instanceof ArrayAccess) { - return $values; - } - - return $values[$i % count($values)]; -} - function twig_strtr($pattern, $replacements) { return str_replace(array_keys($replacements), array_values($replacements), $pattern); diff --git a/test/Twig/Tests/Fixtures/filters/cycle.test b/test/Twig/Tests/Fixtures/functions/cycle.test similarity index 79% rename from test/Twig/Tests/Fixtures/filters/cycle.test rename to test/Twig/Tests/Fixtures/functions/cycle.test index d136833..522a63b 100644 --- a/test/Twig/Tests/Fixtures/filters/cycle.test +++ b/test/Twig/Tests/Fixtures/functions/cycle.test @@ -1,8 +1,8 @@ --TEST-- -"cycle" filter +"cycle" function --TEMPLATE-- {% for i in 0..6 %} -{{ array1|cycle(i) }}-{{ array2|cycle(i) }} +{{ cycle(array1, i) }}-{{ cycle(array2, i) }} {% endfor %} --DATA-- return array('array1' => array('odd', 'even'), 'array2' => array('apple', 'orange', 'citrus')) diff --git a/test/Twig/Tests/Fixtures/tags/include/only.test b/test/Twig/Tests/Fixtures/tags/include/only.test index 3f9a6a8..c6e6b23 100644 --- a/test/Twig/Tests/Fixtures/tags/include/only.test +++ b/test/Twig/Tests/Fixtures/tags/include/only.test @@ -10,7 +10,7 @@ --DATA-- return array('foo' => 'bar') --EXPECT-- -fn_range,fn_constant,foo,_parent, -fn_range,fn_constant,_parent, -fn_range,fn_constant,foo,foo1,_parent, -fn_range,fn_constant,foo1,_parent, +fn_range,fn_constant,fn_cycle,foo,_parent, +fn_range,fn_constant,fn_cycle,_parent, +fn_range,fn_constant,fn_cycle,foo,foo1,_parent, +fn_range,fn_constant,fn_cycle,foo1,_parent,