mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-12 18:36:53 +00:00
add support for the class constant on objects
This commit is contained in:
committed by
Fabien Potencier
parent
59d5260c33
commit
0d6bd45844
@@ -1,6 +1,7 @@
|
||||
# 3.3.9 (2022-XX-XX)
|
||||
|
||||
* Fix custom escapers when using multiple Twig environments
|
||||
* Add support for "constant('class', object)"
|
||||
|
||||
# 3.3.8 (2022-02-04)
|
||||
|
||||
|
||||
@@ -14,6 +14,12 @@ You can read constants from object instances as well:
|
||||
|
||||
{{ constant('RSS', date) }}
|
||||
|
||||
Retrieve the fully qualified class name of an object:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{ constant('class', date) }}
|
||||
|
||||
Use the ``defined`` test to check if a constant is defined:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
@@ -1359,6 +1359,10 @@ function twig_source(Environment $env, $name, $ignoreMissing = false)
|
||||
function twig_constant($constant, $object = null)
|
||||
{
|
||||
if (null !== $object) {
|
||||
if ('class' === $constant) {
|
||||
return \get_class($object);
|
||||
}
|
||||
|
||||
$constant = \get_class($object).'::'.$constant;
|
||||
}
|
||||
|
||||
@@ -1376,6 +1380,10 @@ function twig_constant($constant, $object = null)
|
||||
function twig_constant_is_defined($constant, $object = null)
|
||||
{
|
||||
if (null !== $object) {
|
||||
if ('class' === $constant) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$constant = \get_class($object).'::'.$constant;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
--TEMPLATE--
|
||||
{{ constant('DATE_W3C') == expect ? 'true' : 'false' }}
|
||||
{{ constant('ARRAY_AS_PROPS', object) }}
|
||||
{{ constant('class', object) }}
|
||||
--DATA--
|
||||
return ['expect' => DATE_W3C, 'object' => new \ArrayObject(['hi'])]
|
||||
--EXPECT--
|
||||
true
|
||||
2
|
||||
ArrayObject
|
||||
|
||||
Reference in New Issue
Block a user