diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index fe5ed184d35..dd72f3fb4ab 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -1673,7 +1673,7 @@ public static function getAttribute(Environment $env, Source $source, $object, $ $env->getExtension(SandboxExtension::class)->checkPropertyAllowed($object, $item, $lineno, $source); } - return $object->$item; + return isset($object->$item) ? $object->$item : ((array) $object)[(string) $item]; } } diff --git a/tests/Fixtures/expressions/attributes.test b/tests/Fixtures/expressions/attributes.test new file mode 100644 index 00000000000..9be8197a610 --- /dev/null +++ b/tests/Fixtures/expressions/attributes.test @@ -0,0 +1,13 @@ +--TEST-- +"." notation +--TEMPLATE-- +{{ property.foo }} +{{ date.timezone }} +--DATA-- +return [ + 'date' => new \DateTimeImmutable('now', new \DateTimeZone('Europe/Paris')), + 'property' => (object) array('foo' => 'bar'), +] +--EXPECT-- +bar +Europe/Paris