From: Maarten de Keizer Date: Sat, 26 May 2012 18:45:50 +0000 (+0200) Subject: Add abs filter X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=f0a00368924c385a0c55bba53e0fc1098fb4eab4;p=web%2Fkonrad%2Ftwig.git Add abs filter including unit test and documentation --- diff --git a/doc/filters/abs.rst b/doc/filters/abs.rst new file mode 100644 index 0000000..3a82f62 --- /dev/null +++ b/doc/filters/abs.rst @@ -0,0 +1,18 @@ +``abs`` +======= + +The ``abs`` filter returns the absolute value. + +.. code-block:: jinja + + {# number = -5 #} + + {{ number|abs }} + + {# outputs 5 #} + +.. note:: + + Internally, Twig uses the PHP `abs`_ function. + +.. _`abs`: http://php.net/abs diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index e72dc5e..32606a3 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -126,6 +126,7 @@ class Twig_Extension_Core extends Twig_Extension 'format' => new Twig_Filter_Function('sprintf'), 'replace' => new Twig_Filter_Function('strtr'), 'number_format' => new Twig_Filter_Function('twig_number_format_filter', array('needs_environment' => true)), + 'abs' => new Twig_Filter_Function('abs'), // encoding 'url_encode' => new Twig_Filter_Function('twig_urlencode_filter'), diff --git a/test/Twig/Tests/Fixtures/filters/abs.test b/test/Twig/Tests/Fixtures/filters/abs.test new file mode 100644 index 0000000..27e93fd --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/abs.test @@ -0,0 +1,30 @@ +--TEST-- +"abs" filter +--TEMPLATE-- +{{ (-5.5)|abs }} +{{ (-5)|abs }} +{{ (-0)|abs }} +{{ 0|abs }} +{{ 5|abs }} +{{ 5.5|abs }} +{{ number1|abs }} +{{ number2|abs }} +{{ number3|abs }} +{{ number4|abs }} +{{ number5|abs }} +{{ number6|abs }} +--DATA-- +return array('number1' => -5.5, 'number2' => -5, 'number3' => -0, 'number4' => 0, 'number5' => 5, 'number6' => 5.5) +--EXPECT-- +5.5 +5 +0 +0 +5 +5.5 +5.5 +5 +0 +0 +5 +5.5