canni canni - 6 months ago 67
PHP Question

How to access class constants in Twig?

I have a few class constants in my entity class, e.g.:

class Entity {
const TYPE_PERSON = 0;
const TYPE_COMPANY = 1;
}


In normal PHP I often do
if($var == Entity::TYPE_PERSON)
and I would like to do this kind of stuff in Twig. Is it possible?

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

See documentation for the constant function and the constant test.