dpolac / twig-const
Twig operator to access class constants
Installs: 25
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/dpolac/twig-const
Requires
- php: ^5.4 || ^7.0
- twig/twig: ^1.0
This package is auto-updated.
Last update: 2025-10-29 02:21:57 UTC
README
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' }}
Installation
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 } ]
Using different operators
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 }}