How to access class constants in Twig?

后端 未结 7 1032
醉梦人生
醉梦人生 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:38

    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:

    Displaying the {{ constant('NUM_ITEMS', post) }} most recent results.

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

提交回复
热议问题