Twig

灵活、快速且安全
PHP 模板引擎

a Symfony Product
Docs Functions constant
您正在阅读 Twig 3.x 的文档。切换到 Twig 1.x, 2.x 的文档。

问题与反馈

许可

Twig 文档 根据新的 BSD 许可获得许可。

constant

constant 返回给定字符串的常量值

1
2
{{ some_date|date(constant('DATE_W3C')) }}
{{ constant('Namespace\\Classname::CONSTANT_NAME') }}

您也可以从对象实例中读取常量

1
{{ constant('RSS', date) }}

检索对象的完全限定类名

1
{{ constant('class', date) }}

使用 defined 测试来检查常量是否已定义

1
2
3
{% if constant('SOME_CONST') is defined %}
    ...
{% endif %}