converted the range filter to a function

This commit is contained in:
Fabien Potencier
2010-12-20 08:36:03 +01:00
parent 508a241179
commit 8fe302bb3e
5 changed files with 64 additions and 48 deletions
+1
View File
@@ -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:
+41 -37
View File
@@ -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
----------
+17 -6
View File
@@ -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'),
@@ -85,6 +84,23 @@ class Twig_Extension_Core extends Twig_Extension
return $filters;
}
/**
* 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.
*
@@ -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) {
+1 -1
View File
@@ -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'))
@@ -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,