added the floor filter
authorfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Mon, 14 Dec 2009 19:29:23 +0000 (19:29 +0000)
committerfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Mon, 14 Dec 2009 19:29:23 +0000 (19:29 +0000)
git-svn-id: http://svn.twig-project.org/trunk@184 93ef8e89-cb99-4229-a87c-7fa0fa45744b

CHANGELOG
doc/02-Twig-for-Template-Designers.markdown
lib/Twig/Extension/Core.php

index 18ce387..6949c3b 100644 (file)
--- 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
 
index 83ce5bb..17e1f7c 100644 (file)
@@ -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.
index 5ab0138..3da9c72 100644 (file)
@@ -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);