Search code examples
symfonytwigconstants

Accessing constant values in Twig


In one of my entity, I created these constants to emulate an "enum":

class MyClass {

  // Annotations   

  /**
   * @ORM\Column(type="integer")
   */
  private $status;

  // Status values
  const Created = 10;
  const Refused = 20;
  const Valid   = 30;

  // Getters, setters
}

I can access these values using MyClass::Status, MyClass::Created, MyClass::Refused, as if it were an enumeration.

I want to check what the current status of my entities is in my templates.

I have tried, without success:

{% if entity.status == entity.Created %}
 

Which does not work as expected.


Solution

  • The more elegant solution (as found in the Twig documentation, thanks to Ahmed Siouani) is :

    {% if entity.status is constant('Created', entity) %}