Raphael_b
Raphael_b

Reputation: 1510

Twig: if is_granted('ROLE_MANAGER') check is not granted

I want to check if a role is not granted. I have to display something only for USER but MANAGER is the hierarchy above.

To get that I am doing:

{% if is_granted('ROLE_MANAGER') %}
{% else %}
      my message 
{% endif %}

Which is not really nice. What can be the correct syntax for:

{% if is_NOT_granted('ROLE_MANAGER') %} 

ideas?

Upvotes: 29

Views: 58047

Answers (3)

MouradK
MouradK

Reputation: 1567

Or again

{% if not is_granted('ROLE_MANAGER') %}

   my message 
{% endif %}

Upvotes: 77

Rvanlaak
Rvanlaak

Reputation: 3085

You can also use:

{{ is_granted('ROLE_MANAGER') ? 'true message' : 'false message' }}

or to leave the true output empty:

{{ is_granted('ROLE_MANAGER') == false ? 'false message' }}

Upvotes: 3

Matteo
Matteo

Reputation: 39390

You can simply check as follow:

 {% if is_granted('ROLE_MANAGER') == false %}
                  my message 
 {% endif %}

Hope this help

Upvotes: 30

Related Questions