added support for object instances as the second argument of the constant test (closes #1100)

This commit is contained in:
Fabien Potencier
2013-05-23 18:28:35 +02:00
parent ba88c75557
commit 6939565867
4 changed files with 26 additions and 1 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.13.1 (2013-XX-XX)
* added support for object instances as the second argument of the constant test
* fixed the include function when used in an assignment
* 1.13.0 (2013-05-10)
+11
View File
@@ -1,6 +1,9 @@
``constant``
============
.. versionadded: 1.13.1
constant now accepts object instances as the second argument.
``constant`` checks if a variable has the exact same value as a constant. You
can use either global constants or class constants:
@@ -9,3 +12,11 @@ can use either global constants or class constants:
{% if post.status is constant('Post::PUBLISHED') %}
the status attribute is exactly the same as Post::PUBLISHED
{% endif %}
You can test constants from object instances as well:
.. code-block:: jinja
{% if post.status is constant('PUBLISHED', post) %}
the status attribute is exactly the same as Post::PUBLISHED
{% endif %}
@@ -28,6 +28,17 @@ class Twig_Node_Expression_Test_Constant extends Twig_Node_Expression_Test
->raw('(')
->subcompile($this->getNode('node'))
->raw(' === constant(')
;
if ($this->getNode('arguments')->hasNode(1)) {
$compiler
->raw('get_class(')
->subcompile($this->getNode('arguments')->getNode(1))
->raw(')."::".')
;
}
$compiler
->subcompile($this->getNode('arguments')->getNode(0))
->raw('))')
;
+3 -1
View File
@@ -4,9 +4,11 @@
{{ 8 is constant('E_NOTICE') ? 'ok' : 'no' }}
{{ 'bar' is constant('TwigTestFoo::BAR_NAME') ? 'ok' : 'no' }}
{{ value is constant('TwigTestFoo::BAR_NAME') ? 'ok' : 'no' }}
{{ 2 is constant('ARRAY_AS_PROPS', object) ? 'ok' : 'no' }}
--DATA--
return array('value' => 'bar');
return array('value' => 'bar', 'object' => new ArrayObject(array('hi')));
--EXPECT--
ok
ok
ok
ok