Twig operator to access class constants
Twig operator to access class constants
Const operator #
let you access class constants through any object
of that class.
<?php
class Message {
const TYPE_INFO = "INFO";
// ...
}
$msg = new Message();
{{ msg # TYPE_INFO }}
{# prints 'INFO' #}
{% if msg.type == msg#TYPE_INFO %}
...
{% endif %}
Note that due to Twig limitations, you must add brackets when
using filters and selection operators on constants.
(But you don’t need to add it with other operators.)
{{ (msg#TYPE_INFO)|upper }}
{{ (msg#TYPE_INFO)[2] }}
{{ (msg#TYPE_INFO).attr }}
{{ msg#TYPE_INFO ~ 'S' }}
Install via Composer:
composer require dpolac/twig-const
Add the extension to Twig:
$twig->addExtension(new \DPolac\TwigConst\ConstExtension());
… or if you use Symfony, add the following to your services.yml
config file:
services:
# ...
dpolac.twig_const.extension:
class: DPolac\TwigConst\ConstExtension
tags: [ { name: twig.extension } ]
To use another operator instead of default #
, pass it as string
to extension constructor.
Example:
$twig->addExtension(new \DPolac\TwigConst\ConstExtension('const'));
$twig->addExtension(new \DPolac\TwigConst\ConstExtension('::'));
{{ msg const TYPE_ERROR }}
{{ msg::TYPE_ERROR }}