Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7968835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:08:25+00:00 2026-06-04T07:08:25+00:00

I want to display something like that: Case 1: logged in as USER @

  • 0

I want to display something like that:

Case 1: “logged in as USER”

@ UserName [ logout ]

No problems here, i just do:

@ {{ app.user.username}} [ <a href=”{{ path(“logout”) }}”>logout</a> ]

Case 2: “logged in as ADMIN”

@ AdminName [ logout ]

The same works here:

@ {{ app.user.username}} [ <a href=”{{ path(“logout”) }}”>logout</a> ]

Case 3: “logged in as ADMIN impersonating a USER”

AdminName @ UserName [ return ]

Now thats a problem:

{{ ??..what here..?? }} @ {{ app.user.username}} [ <a href=”{{ (app.request.getRequestUri ~ ‘?_switch_user=_exit’) }}”>return</a> ]

This is the only solution I know… it seems a lot of code for a sipmle displaying username :/

{# iterating through user roles to find ROLE_PREVIOUS_ADMIN #}
{% for role in app.security.token.roles %}
  {% if role.source is defined %}
    {{ role.source.user.username }}
 {% endif %}
{% endfor %}
@ {{ app.user.username }} [ <a href="{{ (app.request.getRequestUri ~ '?_switch_user=_exit') }}">return</a> ]

Is there any other way? I need a pure TWIG solution -> this is supposed to be part of my main twig template (that is extended by all other templates) -> I can’t add controller code to all actions, just to display username.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:08:26+00:00Added an answer on June 4, 2026 at 7:08 am

    With the idea you have proposed above,.. can you not just create a custom twig extension that encompasses your logic from your twig template so that you can just call myCustomTwigFunction within your twig template and it will output the original users name?

    See http://symfony.com/doc/current/cookbook/templating/twig_extension.html for more info about custom twig extensions

    The code you’d have in your Twig extension file would be…

    $roles = $this->container->get('security.context')->getToken()->getRoles();
    foreach ($roles as $role) {
        if (method_exists($role, 'getSource')) {
            return ($role->getSource()->getUser()->getUsername());
        }
    }
    

    Where $container is a class variable of the DI Container on your twig extension class

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do something like this: [Display(Name = Plain text. <span class=\red strong\>Red
I am learning OOP with python. I want to implement something like to display:
I want display data from database in Listbox...Here is my code, It is not
I have a scrollable area that contains popup menus. Conceptually, something like this: <div
I want to do something like this.. I have a update form in PHP,
i want display 1 record from colums zodys , I'm programint in C# I
I have a simple quiz application and I want display a nice timer /
I have db with this table (TableToDo): http://goo.gl/NlTEk I want display all records in
In the header of the django admin, I want display a link. This link
I want to display some kind of animation when my datagrid is updating. Does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.