2017 © Pedro Peláez
 

library twig-const

Twig operator to access class constants

image

dpolac/twig-const

Twig operator to access class constants

  • Saturday, May 28, 2016
  • by dpolac
  • Repository
  • 1 Watchers
  • 0 Stars
  • 14 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 8 % Grown

The README.md

Twig Const

Twig operator to access class constants, (*1)


Const operator # let you access class constants through any object of that class., (*2)

<?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.), (*3)

    {{ (msg#TYPE_INFO)|upper }}
    {{ (msg#TYPE_INFO)[2] }}
    {{ (msg#TYPE_INFO).attr }}
    {{ msg#TYPE_INFO ~ 'S' }}

Installation

Install via Composer:, (*4)

composer require dpolac/twig-const

Add the extension to Twig:, (*5)

$twig->addExtension(new \DPolac\TwigConst\ConstExtension());

... or if you use Symfony, add the following to your services.yml config file:, (*6)

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., (*7)

Example:, (*8)

$twig->addExtension(new \DPolac\TwigConst\ConstExtension('const'));
$twig->addExtension(new \DPolac\TwigConst\ConstExtension('::'));
{{ msg const TYPE_ERROR }}
{{ msg::TYPE_ERROR }}

The Versions

28/05 2016

dev-master

9999999-dev

Twig operator to access class constants

  Sources   Download

MIT

The Requires

 

by Damian Polac

twig const operator constants

28/05 2016

1.0.0

1.0.0.0

Twig operator to access class constants

  Sources   Download

MIT

The Requires

 

by Damian Polac

twig const operator constants