* {{ 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 %}
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``
~~~~~~~~~~~
...
{% 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
----------
'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'),
}
/**
+ * 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
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) {