28 April 2015

Symfony Twig view conditionally shows current username of user logged in

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 %}