From 87ba224cd4e68fc73758a27874fd83c58bca848d Mon Sep 17 00:00:00 2001 From: fabien Date: Sun, 13 Dec 2009 13:26:07 +0000 Subject: [PATCH] added the range filter git-svn-id: http://svn.twig-project.org/trunk@171 93ef8e89-cb99-4229-a87c-7fa0fa45744b --- CHANGELOG | 2 +- doc/02-Twig-for-Template-Designers.markdown | 28 +++++++++++++++++++++++++++ lib/Twig/Extension/Core.php | 1 + lib/Twig/runtime.php | 5 ++++ 4 files changed, 35 insertions(+), 1 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index ab3187c..3a1bb94 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/doc/02-Twig-for-Template-Designers.markdown b/doc/02-Twig-for-Template-Designers.markdown index 72386af..fd1a598 100644 --- a/doc/02-Twig-for-Template-Designers.markdown +++ b/doc/02-Twig-for-Template-Designers.markdown @@ -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 diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 37c2c7a..7116bf2 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -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), diff --git a/lib/Twig/runtime.php b/lib/Twig/runtime.php index 16e617e..14235e0 100644 --- a/lib/Twig/runtime.php +++ b/lib/Twig/runtime.php @@ -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 -- 1.7.2.5