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 ++++++- .../{filters => functions}/constant.test | 2 +- .../Twig/Tests/Fixtures/tags/include/only.test | 8 ++++---- 5 files changed, 21 insertions(+), 15 deletions(-) rename test/Twig/Tests/Fixtures/{filters => functions}/constant.test (87%) diff --git a/CHANGELOG b/CHANGELOG index d58f697e4..80b7381ad 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 ae156fcf9..07bbc09bc 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 0be6f31be..faa77cfc4 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/functions/constant.test similarity index 87% rename from test/Twig/Tests/Fixtures/filters/constant.test rename to test/Twig/Tests/Fixtures/functions/constant.test index d63003801..dbff4abb3 100644 --- a/test/Twig/Tests/Fixtures/filters/constant.test +++ b/test/Twig/Tests/Fixtures/functions/constant.test @@ -1,7 +1,7 @@ --TEST-- "constant" filter --TEMPLATE-- -{{ date|date('DATE_W3C'|constant) }} +{{ date|date(constant('DATE_W3C')) }} --DATA-- $d = date_default_timezone_get(); date_default_timezone_set('UTC'); diff --git a/test/Twig/Tests/Fixtures/tags/include/only.test b/test/Twig/Tests/Fixtures/tags/include/only.test index 9f66ea6ba..3f9a6a8fa 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,