converted the constant filter to a function

This commit is contained in:
Fabien Potencier
2010-12-20 08:40:02 +01:00
parent 8fe302bb3e
commit 47a73476e0
5 changed files with 21 additions and 15 deletions
+1
View File
@@ -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:
+9 -9
View File
@@ -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
----------
+6 -1
View File
@@ -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.
*
@@ -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');
@@ -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,