We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    Based on Everett’s work on the Wiki and suggestions for the docs, I’ve put up a snippet FAQ on Bob’s Guides: http://bobsguides.com/modx-snippet-faq.html.

    I tried to bring together stuff that was kind of scattered in the docs. Hopefully, it will head off some support requests and serve as a place to refer people to.

    Let me know if you see any errors on the page or have suggestions for additions or improvements.
      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
      • 7231
      • 4,205 Posts
      Great FAQ. Plenty of interesting tips. grin
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don't know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 30223
        • 1,010 Posts
        Great faq. One suggestion for "How can I access a parameter in the snippet call from inside the snippet. "

        You can also access parameters of the current snippet from $modx->event->params. This is an associative array with the snippet parameter name as the key. This can be useful to access snippet parameter values from inside a function for instance. The one thing to note of course is that any changes made to the regular php variable is not going to be reflected in the modx->event->params array or visa versa (this may not be as obvious as it sounds to some)
          • 9207 ☆ A M B ☆
          • 2,475 Posts
          Thanks Bob! That’s a great resource!
            • 3749
            • 24,544 Posts
            Quote from: TobyL at Jan 24, 2009, 08:34 AM

            Great faq. One suggestion for "How can I access a parameter in the snippet call from inside the snippet. "

            You can also access parameters of the current snippet from $modx->event->params. This is an associative array with the snippet parameter name as the key. This can be useful to access snippet parameter values from inside a function for instance. The one thing to note of course is that any changes made to the regular php variable is not going to be reflected in the modx->event->params array or visa versa (this may not be as obvious as it sounds to some)

            Great suggestion. Done.! Thanks. grin

            I also added more info about using classes and PHP4/PHP5 compatibility.
              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
              • 20413
              • 2,877 Posts
              THANK YOU!!  :-*

              Modx rocks because of contributions just like this!  8)  

              //One thing to maybe add (unless its in there or the wiki and just basic) is what
              ZAP told me; to add a "return" outside the function though modx otherwise would
              output 1 for true... http://modxcms.com/forums/index.php/topic,31363.msg190209.html#msg190209
                @hawproductions | http://mrhaw.com/

                Infograph: MODX Advanced Install in 7 steps:
                http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

                Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
                http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
                • 7231
                • 4,205 Posts
                You can also access parameters of the current snippet from $modx->event->params.
                I did not know this. So if I have a parameter named MyParam I can access it from within the snippet as $modx->event->MyParam? What is the event clause for? Can you show a quick example of this in action and how to access it in the array?
                  [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                  Something is happening here, but you don't know what it is.
                  Do you, Mr. Jones? - [bob dylan]
                  • 3749
                  • 24,544 Posts
                  Quote from: dev_cw at Jan 24, 2009, 06:23 PM

                  You can also access parameters of the current snippet from $modx->event->params.
                  I did not know this. So if I have a parameter named MyParam I can access it from within the snippet as $modx->event->MyParam?

                  Almost:
                  $someVariable = $modx->event->params['myParam'];


                  What is the event clause for?

                  I have a guess, but I might as well let someone who knows for sure answer this. The array is set in the document parser for modules, plugins, and snippets but, as far as I can tell, it’s never used in the MODx code.
                    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
                    • 30223
                    • 1,010 Posts
                    Quote from: dev_cw at Jan 24, 2009, 06:23 PM

                    You can also access parameters of the current snippet from $modx->event->params.
                    I did not know this. So if I have a parameter named MyParam I can access it from within the snippet as $modx->event->MyParam? What is the event clause for? Can you show a quick example of this in action and how to access it in the array?

                    As Bob said it’s $modx->event->params[’myParam’]. I think it goes back all the way to etomite days and I have no idea what the original idea behind it was. That the event object is used I don’t think is significant. It’s merely a convenience, and a confusing one at that. I’ve found it extremely handy though on several occasions.

                    An example of it’s use is with ’event’ functions in eForm for instance. If in your function you want to know if a form is posted you need to look at the $_POST array as well as check the &formid parameter (as otherwise you may be acting on posted values of a second form on the same page). &formid is not available within the scope of an event function so being able to read it using $modx->event->params[’formid’] is very convenient.

                    <?php
                    function someEvent( &$fields){
                        global $modx;
                    
                        $postback = ( count($_POST)>0 && $fields['formid']== $modx->event->params['formid'] );
                        if($postback){
                            //do something
                        }else{
                            //do something else
                        }
                        return true;
                    }
                    ?>