php 如何访问 Twig 中的类常量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7611086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:02:16  来源:igfitidea点击:

How to access class constants in Twig?

phptemplatestwig

提问by canni

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 中,我经常这样做if($var == Entity::TYPE_PERSON),我想在 Twig 中做这种事情。是否可以?

回答by message

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

只是为了节省您的时间。如果需要访问命名空间下的类常量,请使用

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

回答by NikiC

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

See documentation for the constantfunctionand the constanttest.

请参阅constant函数constant测试的文档。

回答by Alexander Fedorov

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

从 1.12.1 开始,您也可以从对象实例中读取常量:

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

回答by Dmitriy Apollonin

If you are using namespaces

如果您使用命名空间

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

Important! Use double slashes, instead of single

重要的!使用双斜线,而不是单斜线

回答by Damian Polac

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:

创建并注册 Twig 扩展:

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:

现在您可以在 Twig 中使用常量,例如:

{{ MyClass.MY_CONSTANT }}

回答by Chrysweel

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

在 Symfony 的最佳实践一书中,有一个部分涉及这个问题:

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

例如,由于 constant() 函数,可以在您的 Twig 模板中使用常量:

// 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

这里的链接:http: //symfony.com/doc/current/best_practices/configuration.html#constants-vs-configuration-options

回答by Damian Polac

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

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

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

它定义了新的 Twig 运算符#,让您可以通过该类的任何对象访问该类常量。

Use it like that:

像这样使用它:

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

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