How to access class constants in Twig?

PhpTemplatesTwig

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

Just to save your time. If you need to access class constants under namespace, use

{{ constant('Acme\\DemoBundle\\Entity\\Demo::MY_CONSTANT') }}

Solution 2 - Php

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

See documentation for the constant function and the constant test.

Solution 3 - Php

As of 1.12.1 you can read constants from object instances as well:

{% if var == constant('TYPE_PERSON', entity)

Solution 4 - Php

If you are using namespaces

{{ constant('Namespace\\Entity::TYPE_COMPANY') }}

Important! Use double slashes, instead of single

Solution 5 - Php

Edit: I've found better solution, read about it here.



Let's say you have class:

namespace MyNamespace;
class MyClass
{
    const MY_CONSTANT = 'my_constant';
    const MY_CONSTANT2 = 'const2';
}

Create and register Twig extension:

class MyClassExtension extends \Twig_Extension
{
    public function getName()
    { 
        return 'my_class_extension'; 
    }

    public function getGlobals()
    {
        $class = new \ReflectionClass('MyNamespace\MyClass');
        $constants = $class->getConstants();

        return array(
            'MyClass' => $constants
        );
    }
}

Now you can use constants in Twig like:

{{ MyClass.MY_CONSTANT }}

Solution 6 - Php

In book best practices of Symfony there is a section with this issue:

>Constants can be used for example in your Twig templates thanks to the constant() function:

// src/AppBundle/Entity/Post.php
namespace AppBundle\Entity;

class Post
{
    const NUM_ITEMS = 10;

   // ...
}

And use this constant in template twig:

<p>
    Displaying the {{ constant('NUM_ITEMS', post) }} most recent results.
</p>

Here the link: http://symfony.com/doc/current/best_practices/configuration.html#constants-vs-configuration-options

Solution 7 - Php

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 %}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncanniView Question on Stackoverflow
Solution 1 - PhpmessageView Answer on Stackoverflow
Solution 2 - PhpNikiCView Answer on Stackoverflow
Solution 3 - PhpalexfvView Answer on Stackoverflow
Solution 4 - PhpDmitriy ApolloninView Answer on Stackoverflow
Solution 5 - PhpDamian PolacView Answer on Stackoverflow
Solution 6 - PhpChrysweelView Answer on Stackoverflow
Solution 7 - PhpDamian PolacView Answer on Stackoverflow