From 5c35e8a0d76786c8653c4bcb7501a37a426f3351 Mon Sep 17 00:00:00 2001 From: fabien Date: Mon, 14 Dec 2009 19:29:23 +0000 Subject: [PATCH] added the floor filter git-svn-id: http://svn.twig-project.org/trunk@184 93ef8e89-cb99-4229-a87c-7fa0fa45744b --- CHANGELOG | 2 +- doc/02-Twig-for-Template-Designers.markdown | 11 ++++++++--- lib/Twig/Extension/Core.php | 10 ++++++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 18ce387..6949c3b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -18,7 +18,7 @@ environment constant by the "needs_environment" option: * included the default filter function definitions in the extension class files directly (Core, Escaper) * added the .. operator (as a syntactic sugar for the range filter when the step is 1) * added the in operator (as a syntactic sugar for the in filter) - * added the following filters in the Core extension: in, range + * added the following filters in the Core extension: in, range, floor * 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 83ce5bb..17e1f7c 100644 --- a/doc/02-Twig-for-Template-Designers.markdown +++ b/doc/02-Twig-for-Template-Designers.markdown @@ -729,9 +729,6 @@ but exists for completeness' sake. The following operators are supported: * `/`: Divide two numbers. The return value will be a floating point number. `{{ 1 / 2 }}` is `{{ 0.5 }}`. - * `//`: Divide two numbers and return the truncated integer result. `{{ 20 // - 7 }}` is `2`. - * `%`: Calculate the remainder of an integer division. `{{ 11 % 7 }}` is `4`. * `*`: Multiply the left operand with the right one. `{{ 2 * 2 }}` would @@ -830,6 +827,14 @@ otherwise: [twig] {{ var|odd ? 'odd' : 'even' }} +### `floor` + +The `floor` filter divides two numbers and return the truncated integer +result + + [twig] + {{ 20|floor(7) }} {# returns 2 #} + ### `encoding` The `encoding` filter URL encode a given string. diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 5ab0138..3da9c72 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -65,8 +65,9 @@ class Twig_Extension_Core extends Twig_Extension 'format' => new Twig_Filter_Function('sprintf'), // numbers - 'even' => new Twig_Filter_Function('twig_is_even_filter'), - 'odd' => new Twig_Filter_Function('twig_is_odd_filter'), + 'even' => new Twig_Filter_Function('twig_is_even_filter'), + 'odd' => new Twig_Filter_Function('twig_is_odd_filter'), + 'floor' => new Twig_Filter_Function('twig_floor_filter'), // encoding 'urlencode' => new Twig_Filter_Function('twig_urlencode_filter'), @@ -181,6 +182,11 @@ function twig_is_odd_filter($value) return $value % 2 == 1; } +function twig_floor_filter($value, $div) +{ + return floor($value / $div); +} + function twig_length_filter($thing) { return is_string($thing) ? strlen($thing) : count($thing); -- 1.7.2.5