项目作者: dpolac

项目描述 :
Twig operator to access class constants
高级语言: PHP
项目地址: git://github.com/dpolac/twig-const.git
创建时间: 2016-05-28T12:34:22Z
项目社区:https://github.com/dpolac/twig-const

开源协议:MIT License

下载


Twig Const

Twig operator to access class constants


Const operator # let you access class constants through any object
of that class.

  1. <?php
  2. class Message {
  3. const TYPE_INFO = "INFO";
  4. // ...
  5. }
  6. $msg = new Message();
  1. {{ msg # TYPE_INFO }}
  2. {# prints 'INFO' #}
  1. {% if msg.type == msg#TYPE_INFO %}
  2. ...
  3. {% 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.)

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

Installation

Install via Composer:

  1. composer require dpolac/twig-const

Add the extension to Twig:

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

… or if you use Symfony, add the following to your services.yml config file:

  1. services:
  2. # ...
  3. dpolac.twig_const.extension:
  4. class: DPolac\TwigConst\ConstExtension
  5. tags: [ { name: twig.extension } ]

Using different operators

To use another operator instead of default #, pass it as string
to extension constructor.

Example:

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