How to access class constants in Twig?

后端 未结 7 1035
醉梦人生
醉梦人生 2020-12-12 15:49

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

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

In normal PH

7条回答
  •  醉梦人生
    2020-12-12 16:42

    After some years I realized that my previous answer is not really so good. I have created extension that solves problem better. It's published as open source.

    https://github.com/dpolac/twig-const

    It defines new Twig operator # which let you access the class constant through any object of that class.

    Use it like that:

    {% if entity.type == entity#TYPE_PERSON %}

提交回复
热议问题