mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-16 04:16:28 +00:00
added the range filter
git-svn-id: http://svn.twig-project.org/trunk@171 93ef8e89-cb99-4229-a87c-7fa0fa45744b
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
* 0.9.5-DEV
|
||||
|
||||
* added the in operator (as a syntactic sugar for the in filter)
|
||||
* added the following filters in the Core extension: in
|
||||
* added the following filters in the Core extension: in, range
|
||||
* added support for arrays (same behavior as in PHP, a mix between lists and dictionaries, arrays and hashes)
|
||||
* enhanced some error messages to provide better feedback in case of parsing errors
|
||||
|
||||
|
||||
@@ -395,6 +395,17 @@ provided in a variable called `users`:
|
||||
>A sequence can be either an array or an object implementing the `Iterator`
|
||||
>interface.
|
||||
|
||||
If you do need to iterate over a sequence of numbers, you can use the `range`
|
||||
filter:
|
||||
|
||||
[twig]
|
||||
{% for i in 0|range(10) %}
|
||||
* {{ i }}
|
||||
{% endfor %}
|
||||
|
||||
The above snippet of code would print all numbers from 0 to 9 (the high value
|
||||
is never part of the generated array).
|
||||
|
||||
Inside of a `for` loop block you can access some special variables:
|
||||
|
||||
| Variable | Description
|
||||
@@ -864,6 +875,23 @@ The `in` operator is a syntactic sugar for the `in` filter:
|
||||
TRUE
|
||||
{% endif %}
|
||||
|
||||
### `range`
|
||||
|
||||
Returns a list containing a sequence of numbers. The filtered value represents
|
||||
the low value and the filter takes two arguments: the first one is mandatory
|
||||
are represents the high value, and the second one is optional and represents
|
||||
the step (which defaults to `1`).
|
||||
|
||||
If you do need to iterate over a sequence of numbers:
|
||||
|
||||
[twig]
|
||||
{% for i in 0|range(10) %}
|
||||
* {{ i }}
|
||||
{% endfor %}
|
||||
|
||||
>**TIP**
|
||||
>The `range` filter works as the native PHP `range` function.
|
||||
|
||||
### `default`
|
||||
|
||||
The `default` filter returns the passed default value if the value is
|
||||
|
||||
@@ -85,6 +85,7 @@ class Twig_Extension_Core extends Twig_Extension
|
||||
'length' => array('twig_length_filter', false),
|
||||
'sort' => array('twig_sort_filter', false),
|
||||
'in' => array('twig_in_filter', false),
|
||||
'range' => array('twig_range_filter', false),
|
||||
|
||||
// iteration and runtime
|
||||
'default' => array('twig_default_filter', false),
|
||||
|
||||
@@ -105,6 +105,11 @@ function twig_in_filter($value, $compare)
|
||||
return false;
|
||||
}
|
||||
|
||||
function twig_range_filter($start, $end, $step = 1)
|
||||
{
|
||||
return range($start, $end, $step);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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
|
||||
|
||||
Reference in New Issue
Block a user