From 6aa06dea0571f838cdb00eb5a78906828559438c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9goire=20Pineau?= Date: Sat, 23 Nov 2013 01:00:49 -0300 Subject: [PATCH] Added min and max function --- CHANGELOG | 1 + doc/functions/index.rst | 2 ++ doc/functions/max.rst | 15 +++++++++++++++ doc/functions/min.rst | 15 +++++++++++++++ lib/Twig/Extension/Core.php | 2 ++ test/Twig/Tests/Fixtures/functions/max.test | 12 ++++++++++++ test/Twig/Tests/Fixtures/functions/min.test | 12 ++++++++++++ 7 files changed, 59 insertions(+), 0 deletions(-) create mode 100644 doc/functions/max.rst create mode 100644 doc/functions/min.rst create mode 100644 test/Twig/Tests/Fixtures/functions/max.test create mode 100644 test/Twig/Tests/Fixtures/functions/min.test diff --git a/CHANGELOG b/CHANGELOG index 5fd88cd..1d80b36 100644 --- a/CHANGELOG +++ b/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 diff --git a/doc/functions/index.rst b/doc/functions/index.rst index 5955e1f..07214a7 100644 --- a/doc/functions/index.rst +++ b/doc/functions/index.rst @@ -11,6 +11,8 @@ Functions date dump include + max + min parent random range diff --git a/doc/functions/max.rst b/doc/functions/max.rst new file mode 100644 index 0000000..8232b82 --- /dev/null +++ b/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" #} diff --git a/doc/functions/min.rst b/doc/functions/min.rst new file mode 100644 index 0000000..c87cab0 --- /dev/null +++ b/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" #} diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 6e8617c..4e80c67 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -210,6 +210,8 @@ class Twig_Extension_Core extends Twig_Extension 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'), diff --git a/test/Twig/Tests/Fixtures/functions/max.test b/test/Twig/Tests/Fixtures/functions/max.test new file mode 100644 index 0000000..e6c94af --- /dev/null +++ b/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 diff --git a/test/Twig/Tests/Fixtures/functions/min.test b/test/Twig/Tests/Fixtures/functions/min.test new file mode 100644 index 0000000..660471c --- /dev/null +++ b/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 -- 1.7.2.5