From 47a73476e03def89dcf86674efaf97e1f30c5e35 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 20 Dec 2010 08:40:02 +0100 Subject: [PATCH] converted the constant filter to a function --- CHANGELOG | 1 + doc/templates.rst | 18 +++++++++--------- lib/Twig/Extension/Core.php | 7 ++++++- test/Twig/Tests/Fixtures/filters/constant.test | 12 ------------ test/Twig/Tests/Fixtures/functions/constant.test | 12 ++++++++++++ test/Twig/Tests/Fixtures/tags/include/only.test | 8 ++++---- 6 files changed, 32 insertions(+), 26 deletions(-) delete mode 100644 test/Twig/Tests/Fixtures/filters/constant.test create mode 100644 test/Twig/Tests/Fixtures/functions/constant.test diff --git a/CHANGELOG b/CHANGELOG index d58f697..80b7381 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,6 +4,7 @@ Backward incompatibilities: * the items filter, which has been deprecated for quite a long time now, has been removed * the range filter has been converted to a function: 0|range(10) -> range(0, 10) + * the constant filter has been converted to a function: {{ some_date|date('DATE_W3C'|constant) }} -> {{ some_date|date(constant('DATE_W3C')) }} Changes: diff --git a/doc/templates.rst b/doc/templates.rst index ae156fc..07bbc09 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -1303,15 +1303,6 @@ the last filter applied to it. {{ var|raw }} {# var won't be escaped #} {% autoescape off %} -``constant`` (new in Twig 0.9.9) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -``constant`` returns the constant value for a given string: - -.. code-block:: jinja - - {{ some_date|date('DATE_W3C'|constant) }} - ``merge`` (new in Twig 0.9.10) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -1456,6 +1447,15 @@ step of 1): {{ i }}, {% endfor %} +``constant`` +~~~~~~~~~~~~ + +``constant`` returns the constant value for a given string: + +.. code-block:: jinja + + {{ some_date|date(constant('DATE_W3C')) }} + Extensions ---------- diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 0be6f31..faa77cf 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -57,7 +57,6 @@ class Twig_Extension_Core extends Twig_Extension 'upper' => new Twig_Filter_Function('strtoupper'), 'lower' => new Twig_Filter_Function('strtolower'), 'striptags' => new Twig_Filter_Function('strip_tags'), - 'constant' => new Twig_Filter_Function('constant'), // array helpers 'join' => new Twig_Filter_Function('twig_join_filter'), @@ -93,6 +92,7 @@ class Twig_Extension_Core extends Twig_Extension { return array( 'fn_range' => new Twig_Function($this, 'getRange'), + 'fn_constant' => new Twig_Function($this, 'getConstant'), ); } @@ -101,6 +101,11 @@ class Twig_Extension_Core extends Twig_Extension return range($start, $end, $step); } + public function getConstant($value) + { + return constant($value); + } + /** * Returns a list of filters to add to the existing list. * diff --git a/test/Twig/Tests/Fixtures/filters/constant.test b/test/Twig/Tests/Fixtures/filters/constant.test deleted file mode 100644 index d630038..0000000 --- a/test/Twig/Tests/Fixtures/filters/constant.test +++ /dev/null @@ -1,12 +0,0 @@ ---TEST-- -"constant" filter ---TEMPLATE-- -{{ date|date('DATE_W3C'|constant) }} ---DATA-- -$d = date_default_timezone_get(); -date_default_timezone_set('UTC'); -$ret = array('date' => strtotime('2005-10-15 10:00:01')); -date_default_timezone_set($d); -return $ret; ---EXPECT-- -2005-10-15T10:00:01+00:00 diff --git a/test/Twig/Tests/Fixtures/functions/constant.test b/test/Twig/Tests/Fixtures/functions/constant.test new file mode 100644 index 0000000..dbff4ab --- /dev/null +++ b/test/Twig/Tests/Fixtures/functions/constant.test @@ -0,0 +1,12 @@ +--TEST-- +"constant" filter +--TEMPLATE-- +{{ date|date(constant('DATE_W3C')) }} +--DATA-- +$d = date_default_timezone_get(); +date_default_timezone_set('UTC'); +$ret = array('date' => strtotime('2005-10-15 10:00:01')); +date_default_timezone_set($d); +return $ret; +--EXPECT-- +2005-10-15T10:00:01+00:00 diff --git a/test/Twig/Tests/Fixtures/tags/include/only.test b/test/Twig/Tests/Fixtures/tags/include/only.test index 9f66ea6..3f9a6a8 100644 --- a/test/Twig/Tests/Fixtures/tags/include/only.test +++ b/test/Twig/Tests/Fixtures/tags/include/only.test @@ -10,7 +10,7 @@ --DATA-- return array('foo' => 'bar') --EXPECT-- -fn_range,foo,_parent, -fn_range,_parent, -fn_range,foo,foo1,_parent, -fn_range,foo1,_parent, +fn_range,fn_constant,foo,_parent, +fn_range,fn_constant,_parent, +fn_range,fn_constant,foo,foo1,_parent, +fn_range,fn_constant,foo1,_parent, -- 1.7.2.5