Skip to content

Commit

Permalink
Added min and max function
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Dec 3, 2013
1 parent cab9fd9 commit 6aa06de
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,5 +1,6 @@
* 1.15.0 (2013-XX-XX)

* added min and max functions
* added the round filter
* fixed a bug that prevented the optimizers to be enabled/disabled selectively
* fixed first and last filters for UTF-8 strings
Expand Down
2 changes: 2 additions & 0 deletions doc/functions/index.rst
Expand Up @@ -11,6 +11,8 @@ Functions
date
dump
include
max
min
parent
random
range
Expand Down
15 changes: 15 additions & 0 deletions doc/functions/max.rst
@@ -0,0 +1,15 @@
``max``
=======

.. versionadded:: 1.15
The ``max`` function was added in Twig 1.15.

``max`` returns the biggest value of a sequence or a set of values:

.. code-block:: jinja
{{ max(1, 3, 2) }}
{{ max([1, 3, 2]) }}
# When called with a mapping, max ignores keys and only compares values.
{{ max({2:"two", 1:"one", 3:"three", 5:"five", 4:"for"}) }} {# return "two" #}
15 changes: 15 additions & 0 deletions doc/functions/min.rst
@@ -0,0 +1,15 @@
``min``
=======

.. versionadded:: 1.15
The ``min`` function was added in Twig 1.15.

``min`` returns the lowest value of a sequence or a set of values:

.. code-block:: jinja
{{ min(1, 3, 2) }}
{{ min([1, 3, 2]) }}
# When called with a mapping, min ignores keys and only compares values.
{{ min({2:"two", 1:"one", 3:"three", 5:"five", 4:"for"}) }} {# return "five" #}
2 changes: 2 additions & 0 deletions lib/Twig/Extension/Core.php
Expand Up @@ -210,6 +210,8 @@ public function getFilters()
public function getFunctions()
{
return array(
new Twig_SimpleFunction('max', 'max'),
new Twig_SimpleFunction('min', 'min'),
new Twig_SimpleFunction('range', 'range'),
new Twig_SimpleFunction('constant', 'twig_constant'),
new Twig_SimpleFunction('cycle', 'twig_cycle'),
Expand Down
12 changes: 12 additions & 0 deletions test/Twig/Tests/Fixtures/functions/max.test
@@ -0,0 +1,12 @@
--TEST--
"max" function
--TEMPLATE--
{{ max([2, 1, 3, 5, 4]) }}
{{ max(2, 1, 3, 5, 4) }}
{{ max({2:"two", 1:"one", 3:"three", 5:"five", 4:"for"}) }}
--DATA--
return array()
--EXPECT--
5
5
two
12 changes: 12 additions & 0 deletions test/Twig/Tests/Fixtures/functions/min.test
@@ -0,0 +1,12 @@
--TEST--
"min" function
--TEMPLATE--
{{ min(2, 1, 3, 5, 4) }}
{{ min([2, 1, 3, 5, 4]) }}
{{ min({2:"two", 1:"one", 3:"three", 5:"five", 4:"for"}) }}
--DATA--
return array()
--EXPECT--
1
1
five

0 comments on commit 6aa06de

Please sign in to comment.