How to access class constants in Twig?

How to access class constants in Twig?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Accessing Class Constants in Twig

When you are working within a PHP application, accessing class constants is second nature. You can easily check if a variable matches a specific type using if ($var == Entity::TYPE_PERSON). The moment you switch context to a Twig template, however, that familiar syntax stops working. Attempting to use {{ Entity::TYPE_PERSON }} or even passing the constant directly in the PHP block results in a fatal error because the compiled Twig code runs in a sandboxed environment where direct class resolution is restricted by default.

To bridge this gap, you need to understand how Twig resolves identifiers and how it interacts with your application's global scope. The core issue isn't that the constants don't exist; it's that Twig deliberately prevents accessing global constants or class constants to avoid security vulnerabilities and unintended side effects in templates. When a template loads, Twig compiles the file into PHP code. If you allow unrestricted access to global classes, an attacker could potentially manipulate your application's logic by defining malicious constants or accessing sensitive internal classes.

The most robust solution is to pass these constants explicitly from your controller or service layer into the Twig context. You treat the constant values not as magic strings or symbols within the template engine, but as regular data variables. This approach maintains the security model of Twig while giving you access to the logic you need in the view.

Here is how you implement this in a Laravel application, which is where this pattern is most common. In your controller action, retrieve the constants from your Entity class and pass them into the view:

public function show($id)
{
    $entityType = Entity::TYPE_PERSON;
    
    return view('welcome', [
        'type_person' => Entity::TYPE_PERSON,
        'type_company' => Entity::TYPE_COMPANY,
    ]);
}

In the Twig template, you no longer reference the class name. Instead, you access the data keys provided by the controller:

{% if user_type == type_person %}
    This is a person entity.
{% else %}
    This is a company entity.
{% endif %}

While this method works perfectly for simple comparisons, it requires you to define every constant you want to use in the template. If your Entity class has dozens of constants, or if you change a constant's value frequently, you risk forgetting to update the controller context. This manual mapping creates maintenance overhead and can lead to inconsistencies between your logic layers.

If you find yourself needing these constants repeatedly across multiple templates or views, passing them manually every time becomes unwieldy. In such cases, you might consider defining a dedicated namespace in Twig that maps directly to your PHP classes, although this requires careful configuration of the TwigEnvironment and is generally discouraged due to security implications regarding global class access.

Another consideration is how these constants behave when combined with other data sources. If you are using an ORM like Eloquent, you might want to ensure that the constant values align with your database schema or API responses. By passing them as integers from the controller, you guarantee type safety at the boundary of your application logic before the data ever reaches the presentation layer.

Ultimately, the separation of concerns remains key. The business logic defining what TYPE_PERSON represents belongs in your PHP models and controllers, not in your templates. Twig should receive clean, contextual data that it can render safely without needing to introspect your internal class structures. This discipline ensures that your templates remain lightweight and focused on presentation, while the heavy lifting of type definitions stays within the application code where it is easier to test and maintain.

Our network