Symfony Twig view conditionally shows current username of user logged in

Symfony PHP Framework

Here is a quick way to access the user object from the Symfony user or the FOSUserBundle.  In this Twig example below is a conditional statement that if the user is authenticated and has a role, it will display “Hi Aaron Belchamber” or whatever the user’s name is.

{% if is_granted("ROLE") %}

    Hi {{ app.user.username }}!

{% endif %}

 

Leave a Reply 0

Your email address will not be published. Required fields are marked *