2
votes

How can I use a class constant as key in a Twig array/object/hashmap? When I try this

{ constant('App\\Entity\\User::FOO'): 'bar' }

... I'm getting this error:

A hash key must be followed by a colon (:). Unexpected token "punctuation" of value "(" ("punctuation" expected with value ":").

However, it does work the other way aroung (i.e. using the constant as value):

{ 'bar': constant('App\\Entity\\User::FOO') }
1

1 Answers

4
votes

Just enclose the constant() in parentheses, like this:

{ (constant('App\\Entity\\User::FOO')): 'bar' }