Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use Multiple Role's In twig (Symfony2)

I am having these 3 role's

1. ROLE_ADMIN
2. ROLE_SAMPLE
3. ROLE_USER

This My twig file

{% if is_granted('ROLE_ADMIN') %}
    <a href="...">Delete</a>
{% endif %}

i need to show the delete link for ROLE_ADMIN and also ROLE_SAMPLE
how do i get it????
above code is to show the delete link for only ROLE_ADMIN
how to add one more role(ROLE_SAMPLE) in that???

like image 479
GOPI Avatar asked Sep 02 '13 05:09

GOPI


2 Answers

see this link: Symfony2 and Twig sidebar

{% if is_granted('ROLE_ADMIN') or is_granted('ROLE_SAMPLE') %}
        <a href="...">Delete</a>
    {% endif %}
like image 83
GOPI Avatar answered Oct 03 '22 01:10

GOPI


At least as of Symfony 3.2.8 you can use an array to list roles. So this should work:

{% if is_granted(['ROLE_ADMIN', 'ROLE_SAMPLE']) %}
    <a href="...">Delete</a>
{% endif %}

I don't know when this was added, but I prefer it to using multiple calls with or.

like image 40
Vic Metcalfe Avatar answered Oct 02 '22 23:10

Vic Metcalfe