We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!

Answered Append Class

    • 40088
    • 708 Posts
    When a user successfully logins on the front-end is there a way to (using the Login Extra or some other method) append a class to say, the body tag? And when the user signs-out to remove that same class?

    I know how to add/remove classes using js but I don't know how to do it based soley on whether someone is signed-in or not.

    This question has been answered by BobRay. See the first response.

      Todd
    • discuss.answer
      • 3749
      • 24,544 Posts
      The only thing I can think of is a little funky. You could put a snippet tag inside the body tag:


      <body class="whatever [[!addClass]]">


      /* addClass snippet */
      return $modx->user->hasSessionContext('web') ? 'YourAddedClass' : '';
      


        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 40088
        • 708 Posts
        Thanks Bob, that does the job.
          Todd
          • 3749
          • 24,544 Posts
          I should mention another way to go. It's a little slower, but arguably more "correct" from a design standpoint.

          You could add a class to the body tag in the template, and alter the CSS on the fly to define what the class does. The snippet tag could go anywhere on the page.


          $visibility = $modx->user->hasSessionContext('web') ? 'block' : 'hidden';
          
          $css = '
             .my_class {
              display:
              ' . $visibility . ';
          }';
          
          $modx->regClientCSS($css);


          That will inject the CSS code at the end of the head section, where it will override any existing CSS loaded earlier.


            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 40088
            • 708 Posts
            That's interesting, I'll give it a try. Thanks.
              Todd