canni canni - 1 year ago 232
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 Source
{% if var == constant('Entity::TYPE_PERSON') %}
{# or #}
{% if var is constant('Entity::TYPE_PERSON') %}

See documentation for the constant function and the constant test.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download