I’m trying to understand how to conditionally show content in a template based on the roles assigned to a user. In the code below, would users with the ROLE_USER
role be able to see the edit and delete button or only users with the ROLE_ADMIN
role? Would they need to be assigned both roles to see those buttons?
{# templates/admin/my_admin_template.html.twig #}
{% if is_granted('ROLE_ADMIN') or is_granted('ROLE_USER') %}
<ul>
{% for ingredient in recipe.ingredients %}
<li>{{ ingredient.name }}</li>
{% endfor %}
</ul>
{% else %}
<p>Ingrediënten niet beschikbaar.</p>
{% endif %}
{% if is_granted('ROLE_ADMIN') %}
<a href="#" class="btn btn-primary">Edit</a>
<a href="#" class="btn btn-danger">Delete</a>
{% endif %}
New contributor
Hendrickson Jr. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.