From 8fe302bb3ee2ec4ec15828c064c5b2265f6a5614 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 20 Dec 2010 08:36:03 +0100 Subject: [PATCH] converted the range filter to a function --- CHANGELOG | 1 + doc/templates.rst | 78 ++++++++++++----------- lib/Twig/Extension/Core.php | 23 +++++-- lib/Twig/Node/Expression/Binary/Range.php | 2 +- test/Twig/Tests/Fixtures/tags/include/only.test | 8 +- 5 files changed, 64 insertions(+), 48 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 53c7bb2..d58f697 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,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) Changes: diff --git a/doc/templates.rst b/doc/templates.rst index 633a2d4..ae156fc 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -511,11 +511,12 @@ The ``..`` operator can take any expression at both sides: * {{ letter }} {% endfor %} -If you need a step different from 1, you can use the ``range`` filter instead: +If you need a step different from 1, you can use the ``range`` function +instead: .. code-block:: jinja - {% for i in 0|range(10, 2) %} + {% for i in range(0, 10, 2) %} * {{ i }} {% endfor %} @@ -1247,41 +1248,6 @@ the length of a string. The ``sort`` filter sorts an array. -``range`` (new in Twig 0.9.5) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Returns a list containing a sequence of numbers. The left side of the filter -represents the low value. The first argument of the filter is mandatory and -represents the high value. The second argument is optional and represents the -step (which defaults to ``1``). - -If you do need to iterate over a sequence of numbers: - -.. code-block:: jinja - - {% for i in 0|range(10) %} - * {{ i }} - {% endfor %} - -.. tip:: - - The ``range`` filter works as the native PHP ``range`` function. - -The ``..`` operator (see above) is a syntactic sugar for the ``range`` filter -(with a step of 1): - -.. code-block:: jinja - - {% for i in 0|range(10) %} - * {{ i }} - {% endfor %} - - {# is equivalent to #} - - {% for i in 0..10 %} - * {{ i }} - {% endfor %} - ``default`` ~~~~~~~~~~~ @@ -1452,6 +1418,44 @@ useful if you use the ``strict_variables`` option: ... {% endif %} +List of Global Functions +------------------------ + +The following functions are available in the global scope by default: + +``range`` +~~~~~~~~~ + +Returns a list containing an arithmetic progression of integers. When step is +given, it specifies the increment (or decrement): + +.. code-block:: jinja + + {% for i in range(0, 3) %} + {{ i }}, + {% endfor %} + + {# returns 0, 1, 2, 3 #} + + {% for i in range(0, 6, 2) %} + {{ i }}, + {% endfor %} + + {# returns 0, 2, 4, 6 #} + +.. tip:: + + The ``range`` function works as the native PHP ``range`` function. + +The ``..`` operator is a syntactic sugar for the ``range`` function (with a +step of 1): + +.. code-block:: jinja + + {% for i in 0..10 %} + {{ i }}, + {% endfor %} + Extensions ---------- diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 056f696..0be6f31 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -64,7 +64,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'), - 'range' => new Twig_Filter_Function('twig_range_filter'), 'cycle' => new Twig_Filter_Function('twig_cycle_filter'), 'merge' => new Twig_Filter_Function('twig_array_merge'), @@ -86,6 +85,23 @@ class Twig_Extension_Core extends Twig_Extension } /** + * Returns a list of global functions to add to the existing list. + * + * @return array An array of global functions + */ + public function getGlobals() + { + return array( + 'fn_range' => new Twig_Function($this, 'getRange'), + ); + } + + public function getRange($start, $end, $step = 1) + { + return range($start, $end, $step); + } + + /** * Returns a list of filters to add to the existing list. * * @return array An array of filters @@ -254,11 +270,6 @@ function twig_in_filter($value, $compare) return false; } -function twig_range_filter($start, $end, $step = 1) -{ - return range($start, $end, $step); -} - function twig_cycle_filter($values, $i) { if (!is_array($values) && !$values instanceof ArrayAccess) { diff --git a/lib/Twig/Node/Expression/Binary/Range.php b/lib/Twig/Node/Expression/Binary/Range.php index b7f1e91..635bb0d 100644 --- a/lib/Twig/Node/Expression/Binary/Range.php +++ b/lib/Twig/Node/Expression/Binary/Range.php @@ -18,7 +18,7 @@ class Twig_Node_Expression_Binary_Range extends Twig_Node_Expression_Binary public function compile($compiler) { $compiler - ->raw('twig_range_filter(') + ->raw('range(') ->subcompile($this->getNode('left')) ->raw(', ') ->subcompile($this->getNode('right')) diff --git a/test/Twig/Tests/Fixtures/tags/include/only.test b/test/Twig/Tests/Fixtures/tags/include/only.test index 22e3d0f..9f66ea6 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-- -foo,_parent, -_parent, -foo,foo1,_parent, -foo1,_parent, +fn_range,foo,_parent, +fn_range,_parent, +fn_range,foo,foo1,_parent, +fn_range,foo1,_parent, -- 1.7.2.5