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


About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

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