How to access class constants in Twig? How to access class constants in Twig? php php

How to access class constants in Twig?


Just to save your time. If you need to access class constants under namespace, use

{{ constant('Acme\\DemoBundle\\Entity\\Demo::MY_CONSTANT') }}


{% if var == constant('Namespace\\Entity::TYPE_PERSON') %}{# or #}{% if var is constant('Namespace\\Entity::TYPE_PERSON') %}

See documentation for the constant function and the constant test.


As of 1.12.1 you can read constants from object instances as well:

{% if var == constant('TYPE_PERSON', entity)