We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13736
    • 345 Posts
    In MODx Evo I was using the Personalize snippet to display one of two chunks based on whether the user was logged in or not.

    What is the best way to setup something like that in Revo?


    A second part of the question is how do I display the full user name in the chunk? In Evo I was using the UserPlaceholder snippet.
      • 3749
      • 24,544 Posts
      This should work with your original tag, but I haven’t tested it. Let me know.

      BTW, it may work strangely when previewing from the manager. Test it with another browser where you’re not logged in.

      <?php
      /* Personalize Snippet for Revolution */
      
      /* ::::::::::::::::::::::::::::::::::::::::
      * Snippet Name: Personalize
      * Short Desc: returns a chunk if the user is logged in, otherwise calls another
      * Version: 3.2
      * Created By: 	Ryan Thrash ([email protected]), and then
      * powered up by kudo ([email protected]), and then
      * updated to work with 0.9.6 by breezer
      * MODx Revolution version by Bob Ray
      *
      * Date: Aug 03, 2006
      *
      * Changelog:
      * Dec 01, 05 -- initial release
      * Jun 19, 06 -- updated description
      * Jul 19, 06 -- hacked by kudo to output chunks
      * Aug 03, 06 -- added placeholder for username
      * Aug 06, 07 -- renamed to LoggedOrNot as its more fitting, it also matches the example
      * Aug 06, 07 -- replaced 2.0 code with modified version found here
      * Sept 26, 10 -- MODx Revolution version
      *::::::::::::::::::::::::::::::::::::::::
      * Description:
      * Checks to see if webusers are logged in and displays yesChunk if the user
      * is logged or noChunk if user is not logged. Insert only the chunk name as
      * param, without {{}}. Can use a placeholder to output the username.
      *
      * TESTED: can be used more than once per page.
      * TESTED: chunks can contain snippets.
      *
      *
      * Params:
      *    &yesChunk [string] [REQUIRED]
      *        Output for LOGGED users
      *
      *    &noChunk [string] [REQUIRED]
      *        Output for NOT logged users
      *
      *    &ph [string] (optional)
      *    Placeholder for placing the username
      *    ATTENTION!: ph placeholder will *not* be set in noChunk!
      * 
      *    &fullName [`0` or `1`] (optional)
      *    Use full name instead of username in placeholder
      *
      *
      * Example Usage:
      *
      *    [[!Personalize?
      *        &yesChunk=`HelloUser`
      *         &noChunk=`Register`
      *         &ph=`name`
      *     ]]
      *
      * Having Chunks named HelloUser and Register, the first will be
      * shown to a user logged on in the current context,
      * the second to other users.
      *
      *:::::::::::::::::::::::::::::::::::::::: */
      
      /* prepare params and variables */
      $output = '';
      $yesChunk = (isset($yesChunk))? $yesChunk : '';
      $noChunk = (isset($noChunk))? $noChunk : '';
      
      
       if ($modx->user->hasSessionContext($modx->context->get('key')) ) {
         $output =  $modx->getChunk($yesChunk);
         if (isset($ph)) {
            if ($fullName) {
                $profile = $modx->user->getOne('Profile');
                $modx->setPlaceholder($ph, $profile->get('fullname'));
            } else {
                $modx->setPlaceholder($ph, $modx->user->get('username'));
            }
         }
      } else {
         $output = $modx->getChunk($noChunk);
      }
      
      return $output;
        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
        • 13736
        • 345 Posts
        Thankyou.

        As I was comparing the new snippet to the old to see the differences I wondered if the IF snippet would work. I tried my hand at setting something up, but I am not sure how to test for being logged in or not.

        Any ideas?
          • 28215
          • 4,149 Posts
          With core output modifiers:

          [[+modx.user.id:if=`[[+modx.user.id]]`:eq:=`0`:then=`Not logged in. <a href="[[~123]]">Login</a>?`:else=`Welcome [[+modx.user.username]]!`]]
          
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 3749
            • 24,544 Posts
            Quote from: splittingred at Sep 27, 2010, 02:41 AM

            With core output filters:

            [[+modx.user.id:if=`[[+modx.user.id]]`:eq:=`0`:then=`Not logged in. <a href="[[~123]]">Login</a>?`:else=`Welcome [[+modx.user.username]]!`]]
            


            Shaun, please correct me if I’m wrong, but I think we decided to call those "output modifiers" (the "output filter" being the thing that processes them). If not, I’ve got some more re-writing to do. wink
              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
              • 28215
              • 4,149 Posts
              No, you are technically correct. Good catch, Bob.
                shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                • 3749
                • 24,544 Posts
                Thanks. smiley

                I’ve updated the snippet above. It’s now tested and improved. I also added a &fullName parameter that makes it use the full name rather than the username for the placeholder.

                I also submitted the Transport Package and added a GitHub repository at http://github.com/BobRay/personalize
                  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
                  • 24531
                  • 213 Posts
                  Quote from: splittingred at Sep 27, 2010, 02:41 AM

                  With core output modifiers:

                  [[+modx.user.id:if=`[[+modx.user.id]]`:eq:=`0`:then=`Not logged in. <a href="[[~123]]">Login</a>?`:else=`Welcome [[+modx.user.username]]!`]]
                  


                  this gets me Welcome (anonymous)!
                  Too bad cause i really miss that PHx syntax. However, it’s working with If snippet
                    • 30585
                    • 833 Posts
                    I’m not sure if it’s too late to reply, but I noticed that when I use [[+username]] instead of [[+modx.user.username]], it displays the real user name, not Anonymous.
                    My guess is that MODx treats [[+modx.user.username]] as a user with manager access credentials and unless that user is also logged in the front-end, their name may not be displayed.[[+username]] outputs the username collected during the user registration process.

                    I may be completely worng in my interpretation of the issue, but when I use the following call, it works for me.

                    [[!If?
                       &subject=`[[+username]]`
                       &operator=`EQ`
                       &operand=``
                       &then=`[[$LoginLink]] [[$SignupLink]]`
                       &else=`Hello [[+username]] [[$ProfileLink]] // [[$LogoutLink]]`
                    ]]


                    Hope it helps.
                      A MODx Fanatic
                      • 20407
                      • 82 Posts
                      ??? Is there no simple/reliable way to tell if the user is logged in? I tried stuff like this:
                      [[!+username:empty=`Not logged in`:notempty=`Logged in`]]
                      

                      But I always get ’logged in’ even in a different browser after clearing all browser sessions. Using [[+modx.user.username]] also doesn’t work as it returns ’anonymous’ as rav3n said.