Twig: show something if user has roleX in a user list (not the current user).









up vote
1
down vote

favorite












In Symfony 4, I have a couple of different roles. I have a view in Twig which shows a user list. Users can have multiple roles. In the list, I want to show some text if a user has a role "MANAGER". Showing all roles is done with:



% for role in user.roles %
role
% endfor %


Now if the user has the role "MANAGER" I want to show some text. I tried:



% for role in user.roles %
% if (role is "MANAGER") %
Show some text.
% endif %
% endfor %


but this returns the error




Unexpected token "string" of value "MANAGER" ("name" expected).




Same error is shown when I use % if is "MANAGER") % and when I use % if "MANAGER") % for some reason Show some text. is shown for every role the user has, no matter which role that is. What am I doing wrong?










share|improve this question

























    up vote
    1
    down vote

    favorite












    In Symfony 4, I have a couple of different roles. I have a view in Twig which shows a user list. Users can have multiple roles. In the list, I want to show some text if a user has a role "MANAGER". Showing all roles is done with:



    % for role in user.roles %
    role
    % endfor %


    Now if the user has the role "MANAGER" I want to show some text. I tried:



    % for role in user.roles %
    % if (role is "MANAGER") %
    Show some text.
    % endif %
    % endfor %


    but this returns the error




    Unexpected token "string" of value "MANAGER" ("name" expected).




    Same error is shown when I use % if is "MANAGER") % and when I use % if "MANAGER") % for some reason Show some text. is shown for every role the user has, no matter which role that is. What am I doing wrong?










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      In Symfony 4, I have a couple of different roles. I have a view in Twig which shows a user list. Users can have multiple roles. In the list, I want to show some text if a user has a role "MANAGER". Showing all roles is done with:



      % for role in user.roles %
      role
      % endfor %


      Now if the user has the role "MANAGER" I want to show some text. I tried:



      % for role in user.roles %
      % if (role is "MANAGER") %
      Show some text.
      % endif %
      % endfor %


      but this returns the error




      Unexpected token "string" of value "MANAGER" ("name" expected).




      Same error is shown when I use % if is "MANAGER") % and when I use % if "MANAGER") % for some reason Show some text. is shown for every role the user has, no matter which role that is. What am I doing wrong?










      share|improve this question













      In Symfony 4, I have a couple of different roles. I have a view in Twig which shows a user list. Users can have multiple roles. In the list, I want to show some text if a user has a role "MANAGER". Showing all roles is done with:



      % for role in user.roles %
      role
      % endfor %


      Now if the user has the role "MANAGER" I want to show some text. I tried:



      % for role in user.roles %
      % if (role is "MANAGER") %
      Show some text.
      % endif %
      % endfor %


      but this returns the error




      Unexpected token "string" of value "MANAGER" ("name" expected).




      Same error is shown when I use % if is "MANAGER") % and when I use % if "MANAGER") % for some reason Show some text. is shown for every role the user has, no matter which role that is. What am I doing wrong?







      symfony twig






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 16:01









      Dirk J. Faber

      1,0881217




      1,0881217






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          As an answer to your self posted answer: a single role is not an array, the containment operator (see https://twig.symfony.com/doc/2.x/templates.html#containment-operator) supports checks for substrings as well, that's what happening here.



          So you check works, but might have false-positives if you have for example a role "MINI_MANAGER", e.g.



          % set role = "MINI_MANAGER" %
          % if "MANAGER" in role %
          Some text here.
          % endif %


          will also output "Some text here.". So the better solution would be:



          % for role in user.roles %
          % if role == "MANAGER" %
          Some text here.
          % endif %
          % endfor %


          This could still lead to problems when role is the boolean value "true" (that is not a Twig problem, but normal PHP behavior), so you can also have a look into the "same as" test, see https://twig.symfony.com/doc/2.x/tests/sameas.html



          % for role in user.roles %
          % if role is same as("MANAGER") %
          Some text here.
          % endif %
          % endfor %





          share|improve this answer




















          • Thank you for correcting me. Although your answer makes perfect sense, I cannot get the desired effect. When I use your code (either the first or second suggestion) no text 'some text here' is rendered. No error either btw.
            – Dirk J. Faber
            Nov 12 at 8:41










          • I am somewhat of an idiot. The role is actually called "ROLE_MANAGER" and not "MANAGER", which is why I did not get the result. Finally figured that out...
            – Dirk J. Faber
            Nov 12 at 10:05

















          up vote
          0
          down vote













          So it seems I have figured it out. It seems every single role is in fact an array, so you have to check for the value within the array like this:



          % for role in user.roles %
          % if "MANAGER" in role %
          Some text here.
          % endif %
          % endfor %


          I am still not sure why a single role is an array though, but there surely is a reason for that.






          share|improve this answer




















          • Can you show how you modeled your User class and how roles are associated to it? This might explain the behaviour.
            – xabbuh
            Nov 12 at 9:47










          • I am using the FOSUser bundle. For new "manager" users I use a lazy way to give them this role with the contruct method: $this->roles = array('ROLE_MANAGER');
            – Dirk J. Faber
            Nov 12 at 10:02










          • @xabbuh, I figured out what I did wrong. The role is not "MANAGER" but "ROLE_MANAGER". That's why it did not work. Such an elementary mistake...
            – Dirk J. Faber
            Nov 12 at 10:06

















          up vote
          0
          down vote













          What about this?



          % if is_granted('ROLE_MANAGER') % 
          Some text here
          % endif %


          Source: Symfony2 security functions in Twig? How to check the user's role?



          See also Symfony Doc




          Roles: When a user logs in, they receive a set of roles (e.g.
          ROLE_ADMIN).







          share|improve this answer




















          • Thank you for the suggestion, but I am not looking for the permissions of the user that is viewing the page, I want to show something in a list of users next to users with the role "MANAGER".
            – Dirk J. Faber
            Nov 11 at 16:50










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250529%2ftwig-show-something-if-user-has-rolex-in-a-user-list-not-the-current-user%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          As an answer to your self posted answer: a single role is not an array, the containment operator (see https://twig.symfony.com/doc/2.x/templates.html#containment-operator) supports checks for substrings as well, that's what happening here.



          So you check works, but might have false-positives if you have for example a role "MINI_MANAGER", e.g.



          % set role = "MINI_MANAGER" %
          % if "MANAGER" in role %
          Some text here.
          % endif %


          will also output "Some text here.". So the better solution would be:



          % for role in user.roles %
          % if role == "MANAGER" %
          Some text here.
          % endif %
          % endfor %


          This could still lead to problems when role is the boolean value "true" (that is not a Twig problem, but normal PHP behavior), so you can also have a look into the "same as" test, see https://twig.symfony.com/doc/2.x/tests/sameas.html



          % for role in user.roles %
          % if role is same as("MANAGER") %
          Some text here.
          % endif %
          % endfor %





          share|improve this answer




















          • Thank you for correcting me. Although your answer makes perfect sense, I cannot get the desired effect. When I use your code (either the first or second suggestion) no text 'some text here' is rendered. No error either btw.
            – Dirk J. Faber
            Nov 12 at 8:41










          • I am somewhat of an idiot. The role is actually called "ROLE_MANAGER" and not "MANAGER", which is why I did not get the result. Finally figured that out...
            – Dirk J. Faber
            Nov 12 at 10:05














          up vote
          1
          down vote



          accepted










          As an answer to your self posted answer: a single role is not an array, the containment operator (see https://twig.symfony.com/doc/2.x/templates.html#containment-operator) supports checks for substrings as well, that's what happening here.



          So you check works, but might have false-positives if you have for example a role "MINI_MANAGER", e.g.



          % set role = "MINI_MANAGER" %
          % if "MANAGER" in role %
          Some text here.
          % endif %


          will also output "Some text here.". So the better solution would be:



          % for role in user.roles %
          % if role == "MANAGER" %
          Some text here.
          % endif %
          % endfor %


          This could still lead to problems when role is the boolean value "true" (that is not a Twig problem, but normal PHP behavior), so you can also have a look into the "same as" test, see https://twig.symfony.com/doc/2.x/tests/sameas.html



          % for role in user.roles %
          % if role is same as("MANAGER") %
          Some text here.
          % endif %
          % endfor %





          share|improve this answer




















          • Thank you for correcting me. Although your answer makes perfect sense, I cannot get the desired effect. When I use your code (either the first or second suggestion) no text 'some text here' is rendered. No error either btw.
            – Dirk J. Faber
            Nov 12 at 8:41










          • I am somewhat of an idiot. The role is actually called "ROLE_MANAGER" and not "MANAGER", which is why I did not get the result. Finally figured that out...
            – Dirk J. Faber
            Nov 12 at 10:05












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          As an answer to your self posted answer: a single role is not an array, the containment operator (see https://twig.symfony.com/doc/2.x/templates.html#containment-operator) supports checks for substrings as well, that's what happening here.



          So you check works, but might have false-positives if you have for example a role "MINI_MANAGER", e.g.



          % set role = "MINI_MANAGER" %
          % if "MANAGER" in role %
          Some text here.
          % endif %


          will also output "Some text here.". So the better solution would be:



          % for role in user.roles %
          % if role == "MANAGER" %
          Some text here.
          % endif %
          % endfor %


          This could still lead to problems when role is the boolean value "true" (that is not a Twig problem, but normal PHP behavior), so you can also have a look into the "same as" test, see https://twig.symfony.com/doc/2.x/tests/sameas.html



          % for role in user.roles %
          % if role is same as("MANAGER") %
          Some text here.
          % endif %
          % endfor %





          share|improve this answer












          As an answer to your self posted answer: a single role is not an array, the containment operator (see https://twig.symfony.com/doc/2.x/templates.html#containment-operator) supports checks for substrings as well, that's what happening here.



          So you check works, but might have false-positives if you have for example a role "MINI_MANAGER", e.g.



          % set role = "MINI_MANAGER" %
          % if "MANAGER" in role %
          Some text here.
          % endif %


          will also output "Some text here.". So the better solution would be:



          % for role in user.roles %
          % if role == "MANAGER" %
          Some text here.
          % endif %
          % endfor %


          This could still lead to problems when role is the boolean value "true" (that is not a Twig problem, but normal PHP behavior), so you can also have a look into the "same as" test, see https://twig.symfony.com/doc/2.x/tests/sameas.html



          % for role in user.roles %
          % if role is same as("MANAGER") %
          Some text here.
          % endif %
          % endfor %






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 0:35









          Kevin

          2014




          2014











          • Thank you for correcting me. Although your answer makes perfect sense, I cannot get the desired effect. When I use your code (either the first or second suggestion) no text 'some text here' is rendered. No error either btw.
            – Dirk J. Faber
            Nov 12 at 8:41










          • I am somewhat of an idiot. The role is actually called "ROLE_MANAGER" and not "MANAGER", which is why I did not get the result. Finally figured that out...
            – Dirk J. Faber
            Nov 12 at 10:05
















          • Thank you for correcting me. Although your answer makes perfect sense, I cannot get the desired effect. When I use your code (either the first or second suggestion) no text 'some text here' is rendered. No error either btw.
            – Dirk J. Faber
            Nov 12 at 8:41










          • I am somewhat of an idiot. The role is actually called "ROLE_MANAGER" and not "MANAGER", which is why I did not get the result. Finally figured that out...
            – Dirk J. Faber
            Nov 12 at 10:05















          Thank you for correcting me. Although your answer makes perfect sense, I cannot get the desired effect. When I use your code (either the first or second suggestion) no text 'some text here' is rendered. No error either btw.
          – Dirk J. Faber
          Nov 12 at 8:41




          Thank you for correcting me. Although your answer makes perfect sense, I cannot get the desired effect. When I use your code (either the first or second suggestion) no text 'some text here' is rendered. No error either btw.
          – Dirk J. Faber
          Nov 12 at 8:41












          I am somewhat of an idiot. The role is actually called "ROLE_MANAGER" and not "MANAGER", which is why I did not get the result. Finally figured that out...
          – Dirk J. Faber
          Nov 12 at 10:05




          I am somewhat of an idiot. The role is actually called "ROLE_MANAGER" and not "MANAGER", which is why I did not get the result. Finally figured that out...
          – Dirk J. Faber
          Nov 12 at 10:05












          up vote
          0
          down vote













          So it seems I have figured it out. It seems every single role is in fact an array, so you have to check for the value within the array like this:



          % for role in user.roles %
          % if "MANAGER" in role %
          Some text here.
          % endif %
          % endfor %


          I am still not sure why a single role is an array though, but there surely is a reason for that.






          share|improve this answer




















          • Can you show how you modeled your User class and how roles are associated to it? This might explain the behaviour.
            – xabbuh
            Nov 12 at 9:47










          • I am using the FOSUser bundle. For new "manager" users I use a lazy way to give them this role with the contruct method: $this->roles = array('ROLE_MANAGER');
            – Dirk J. Faber
            Nov 12 at 10:02










          • @xabbuh, I figured out what I did wrong. The role is not "MANAGER" but "ROLE_MANAGER". That's why it did not work. Such an elementary mistake...
            – Dirk J. Faber
            Nov 12 at 10:06














          up vote
          0
          down vote













          So it seems I have figured it out. It seems every single role is in fact an array, so you have to check for the value within the array like this:



          % for role in user.roles %
          % if "MANAGER" in role %
          Some text here.
          % endif %
          % endfor %


          I am still not sure why a single role is an array though, but there surely is a reason for that.






          share|improve this answer




















          • Can you show how you modeled your User class and how roles are associated to it? This might explain the behaviour.
            – xabbuh
            Nov 12 at 9:47










          • I am using the FOSUser bundle. For new "manager" users I use a lazy way to give them this role with the contruct method: $this->roles = array('ROLE_MANAGER');
            – Dirk J. Faber
            Nov 12 at 10:02










          • @xabbuh, I figured out what I did wrong. The role is not "MANAGER" but "ROLE_MANAGER". That's why it did not work. Such an elementary mistake...
            – Dirk J. Faber
            Nov 12 at 10:06












          up vote
          0
          down vote










          up vote
          0
          down vote









          So it seems I have figured it out. It seems every single role is in fact an array, so you have to check for the value within the array like this:



          % for role in user.roles %
          % if "MANAGER" in role %
          Some text here.
          % endif %
          % endfor %


          I am still not sure why a single role is an array though, but there surely is a reason for that.






          share|improve this answer












          So it seems I have figured it out. It seems every single role is in fact an array, so you have to check for the value within the array like this:



          % for role in user.roles %
          % if "MANAGER" in role %
          Some text here.
          % endif %
          % endfor %


          I am still not sure why a single role is an array though, but there surely is a reason for that.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 16:30









          Dirk J. Faber

          1,0881217




          1,0881217











          • Can you show how you modeled your User class and how roles are associated to it? This might explain the behaviour.
            – xabbuh
            Nov 12 at 9:47










          • I am using the FOSUser bundle. For new "manager" users I use a lazy way to give them this role with the contruct method: $this->roles = array('ROLE_MANAGER');
            – Dirk J. Faber
            Nov 12 at 10:02










          • @xabbuh, I figured out what I did wrong. The role is not "MANAGER" but "ROLE_MANAGER". That's why it did not work. Such an elementary mistake...
            – Dirk J. Faber
            Nov 12 at 10:06
















          • Can you show how you modeled your User class and how roles are associated to it? This might explain the behaviour.
            – xabbuh
            Nov 12 at 9:47










          • I am using the FOSUser bundle. For new "manager" users I use a lazy way to give them this role with the contruct method: $this->roles = array('ROLE_MANAGER');
            – Dirk J. Faber
            Nov 12 at 10:02










          • @xabbuh, I figured out what I did wrong. The role is not "MANAGER" but "ROLE_MANAGER". That's why it did not work. Such an elementary mistake...
            – Dirk J. Faber
            Nov 12 at 10:06















          Can you show how you modeled your User class and how roles are associated to it? This might explain the behaviour.
          – xabbuh
          Nov 12 at 9:47




          Can you show how you modeled your User class and how roles are associated to it? This might explain the behaviour.
          – xabbuh
          Nov 12 at 9:47












          I am using the FOSUser bundle. For new "manager" users I use a lazy way to give them this role with the contruct method: $this->roles = array('ROLE_MANAGER');
          – Dirk J. Faber
          Nov 12 at 10:02




          I am using the FOSUser bundle. For new "manager" users I use a lazy way to give them this role with the contruct method: $this->roles = array('ROLE_MANAGER');
          – Dirk J. Faber
          Nov 12 at 10:02












          @xabbuh, I figured out what I did wrong. The role is not "MANAGER" but "ROLE_MANAGER". That's why it did not work. Such an elementary mistake...
          – Dirk J. Faber
          Nov 12 at 10:06




          @xabbuh, I figured out what I did wrong. The role is not "MANAGER" but "ROLE_MANAGER". That's why it did not work. Such an elementary mistake...
          – Dirk J. Faber
          Nov 12 at 10:06










          up vote
          0
          down vote













          What about this?



          % if is_granted('ROLE_MANAGER') % 
          Some text here
          % endif %


          Source: Symfony2 security functions in Twig? How to check the user's role?



          See also Symfony Doc




          Roles: When a user logs in, they receive a set of roles (e.g.
          ROLE_ADMIN).







          share|improve this answer




















          • Thank you for the suggestion, but I am not looking for the permissions of the user that is viewing the page, I want to show something in a list of users next to users with the role "MANAGER".
            – Dirk J. Faber
            Nov 11 at 16:50














          up vote
          0
          down vote













          What about this?



          % if is_granted('ROLE_MANAGER') % 
          Some text here
          % endif %


          Source: Symfony2 security functions in Twig? How to check the user's role?



          See also Symfony Doc




          Roles: When a user logs in, they receive a set of roles (e.g.
          ROLE_ADMIN).







          share|improve this answer




















          • Thank you for the suggestion, but I am not looking for the permissions of the user that is viewing the page, I want to show something in a list of users next to users with the role "MANAGER".
            – Dirk J. Faber
            Nov 11 at 16:50












          up vote
          0
          down vote










          up vote
          0
          down vote









          What about this?



          % if is_granted('ROLE_MANAGER') % 
          Some text here
          % endif %


          Source: Symfony2 security functions in Twig? How to check the user's role?



          See also Symfony Doc




          Roles: When a user logs in, they receive a set of roles (e.g.
          ROLE_ADMIN).







          share|improve this answer












          What about this?



          % if is_granted('ROLE_MANAGER') % 
          Some text here
          % endif %


          Source: Symfony2 security functions in Twig? How to check the user's role?



          See also Symfony Doc




          Roles: When a user logs in, they receive a set of roles (e.g.
          ROLE_ADMIN).








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 16:37









          baris1892

          447718




          447718











          • Thank you for the suggestion, but I am not looking for the permissions of the user that is viewing the page, I want to show something in a list of users next to users with the role "MANAGER".
            – Dirk J. Faber
            Nov 11 at 16:50
















          • Thank you for the suggestion, but I am not looking for the permissions of the user that is viewing the page, I want to show something in a list of users next to users with the role "MANAGER".
            – Dirk J. Faber
            Nov 11 at 16:50















          Thank you for the suggestion, but I am not looking for the permissions of the user that is viewing the page, I want to show something in a list of users next to users with the role "MANAGER".
          – Dirk J. Faber
          Nov 11 at 16:50




          Thank you for the suggestion, but I am not looking for the permissions of the user that is viewing the page, I want to show something in a list of users next to users with the role "MANAGER".
          – Dirk J. Faber
          Nov 11 at 16:50

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250529%2ftwig-show-something-if-user-has-rolex-in-a-user-list-not-the-current-user%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto