We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • I would advise anyone using conditional architecture to read this:

    http://modx.com/blog/2012/09/14/tags-as-the-result-or-how-conditionals-are-like-mosquitoes/

    I'm not sure that this method has made it to the official documentation.

    Like others, we found a site we developed to be very slow using output modifiers and the If snippet (though we couldn't point to one or the other being the main culprit.

    Once we used this new method, the site sped up significantly. There were a couple of scenarios where it didn't work and we had to use the If snippet, so both methods are very useful.
      • 3749
      • 24,544 Posts
      That article is a great resource.

      The output modifiers are there as a convenience for people who are not comfortable with PHP. There are cases where the speed difference between them and a custom snippet is negligible, but there are other cases where the difference is massive.

      The problem is that all the output modifiers are handled by the same large piece of code. MODX has to parse the tag to figure out which modifiers to apply and then parse them further to analyze the conditions (and maybe pass the results on to another modifier, which also has to be parsed).

      A dedicated snippet knows what it's getting ahead of time, makes a quick decision, and returns the result. For example, this one-line snippet will show a chunk to logged-in users and nothing to everyone else:

      return $modx->user->hasSessionContext('web') ? $modx->getChunk('SomeChunk') : '';


      This is almost certain to be faster than the 649 lines of the modOutputModifier class. wink


      The If snippet, is an intermediate option -- usually faster than an output modifier (sometimes a lot faster), but slightly slower than a custom snippet because it has to parse the tag to see what logical test it needs to perform.

      Since bounce rate effects can be measured in milliseconds, I always use a custom snippet, even in cases where it's probably not necessary. Bob's Guides doesn't use a single output modifier and the If snippet isn't even installed there (though it's an excellent choice if you're not up to creating a custom snippet).
        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
      • Originally PHx was intended for working with placeholders used in the mini-templates of Ditto, Jot and similar snippets, where embedding snippets was not always practical or even possible. To be honest, I have never approved of its use beyond that. I fail to see how it is any easier to learn its syntax than it is to learn the simple PHP that is needed for such snippets as the one BobRay illustrates above. But then I learned to program by writing assembly language utilities using EDLIN, so I'm probably a poor judge of such matters.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 3749
          • 24,544 Posts
          ;) Now that you've said that, let me rewrite the snippet to make it a little easier to follow. It doesn't have to be that cryptic.

          This Snippet tag goes in the page content or in the Template:
          [[!ShowLoggedInChunk]]


          This is the code of the ShowLoggedInChunk snippet:
          /* ShowLoggedInChunk snippet */
          
          /* Everything inside these comment tags will be ignored */
          
          /* This variable will hold the 
             output to return from the snippet;
             Set it to an empty string 
          */
          $output = ""; 
          
          /* See if the current user is logged in */
          
          if ($modx->user->hasSessionContext('web')) { 
          
              /* Yes, set $output to the content
                 of the chunk named 'SomeChunk' */
          
              $output = modx->getChunk('SomeChunk'); 
            
          } else {
          
              /* No, set $output to an empty string */
          
              $output = "";
          }
          
          /* FYI: The whole else clause above is unnecessary, 
             since $output already contains an empty string, 
             but you might want to do something else there, 
             like send the content of a different chunk
           */
          
          /* The Snippet tag will be replaced by whatever 
             is returned. It will be either the empty string
             we started with, or the content of the chunk */
          
          return $output;
          [ed. note: BobRay last edited this post 11 years, 3 months ago.]
            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
            • 42689
            • 125 Posts
            Hello i am newper in MODx .. i want to show the latest first three news and testimonials at right side of my website.. so can some one tell me how can i manage this part form front-nd to back-nd..?

            And also plz tell me how can i manage that with "getResources" ..? i already have installed the pack of "getResources"..

            I means to say from the back-nd ..? how can i enter the news details ..? like headning,sub-heading,posted date etc etc ..? and how can i get those first three latest details in front-nd ..??

            Thanx
              • 37984
              • 215 Posts
              @chirag_wgsol - I think that the first step would be to check the documentation. If that doesn't get you there, then there are quite a few tutorials for using getResources out there.

              http://rtfm.modx.com/display/ADDON/getResources
                Jesse Couch
                MODX Aficionado, Front-End Designer & Developer
                http://www.designcouch.com
                • Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 39501
                  • 163 Posts
                  @BobRay Thanks for posting. Guess I need to brush up on my php skills and stop relying on tags to do the hard work for me.
                    • 3749
                    • 24,544 Posts
                    I didn't mean to sound quite so stringent on the topic. If the page-load times are acceptable, there's nothing inherently wrong with using output modifiers, and the If snippet is almost always fine since the speed difference between If and a dedicated snippet is negligible.
                      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
                      • 3749
                      • 24,544 Posts
                      I didn't mean to sound quite so stringent on the topic. If the page-load times are acceptable, there's nothing inherently wrong with using output modifiers, and the If snippet is almost always fine since the speed difference between If and a dedicated snippet is negligible.
                        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