0

Hi I have a class following:

class allConstants {
   //PREDEFINED ALL FIXED GROUPS  USING IN ACCOUNT MODULE 
    const GROUP_BANK_ACCOUNT            = 'Bank Accounts';
    const GROUP_CURRENT_ASSETS          = 'Current Assets';
    const GROUP_LOAN_LIBILITIES         = 'Loan (Liabilities)';
    const GROUP_BANK_OD_ACCOUNT         = 'Bank OD a/c';
    const GROUP_CASH_IN_HAND  
 }

So I want to access these constants into twig file. So when I am using like following in twig : constant('\Edu\AccountBundle\Constants\allConstants::GROUP_BANK_ACCOUNT');

its showing an refrence erro that "constant" not define. Please guide how to make it work. I am using symfony 2.3.7 Thanks in advance

Sunil Rawat
  • 709
  • 10
  • 41
  • 1
    possible duplicate of [How to access class constants in Twig?](http://stackoverflow.com/questions/7611086/how-to-access-class-constants-in-twig) – Marcel Burkhard Apr 17 '15 at 09:00

2 Answers2

0
{% if gropu is constant('allConstants::GROUP_BANK_ACCOUNT') %}
    the status attribute is exactly the same as allConstants::GROUP_BANK_ACCOUNT
{% endif %}

test constants from object instances

{% if gropu is constant('GROUP_BANK_ACCOUNT', allConstants) %}
    the status attribute is exactly the same as allConstants::GROUP_BANK_ACCOUNT
{% endif %}
Muhammad Ahmed
  • 481
  • 2
  • 14
0

The code posted by Muhammad is right:

{% if group is constant('GROUP_BANK_ACCOUNT', allConstants) %}
    the status attribute is exactly the same as allConstants::GROUP_BANK_ACCOUNT
{% endif %}

But you can use like this only with Twig >=1.12.1; with lower version, you can just use it from static classes, like this:

constant('allConstants::GROUP_BANK_ACCOUNT')
Alessandro Lai
  • 2,254
  • 2
  • 24
  • 32