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

    Is there anything like global placeholders in MODX?
    In one snippet I set a variable which whill be outputen in HTML using chunk triggered by the other snippet. The thing is I would like to get the placeholder value in any other snippet. My simplified idea is to have a snippet which outputs some errors or positive messages which can be outputted afterwards using another snippet (simply because they need to appear in different location than the original snippet is doing its job).

    I set the placeholder like this:
    $modx->setPlaceholder('notification', $error);


    I'm able to get its value in the same snippet by doing so:
    $modx->getPlaceholder('notification');


    In the other snippet Im able to get the chunk with the placeholder value:
    $not_error = $modx->getChunk('notification',array(
    				'notification' => $error,
    		));


    Yet I cannot get the value of the placeholder in any other snippet than the one I set the placeholder originally.

    Does anyone have any ideas?
      • 39932
      • 483 Posts
      You can absolutely get placeholders from other Snippets, but not between HTTP requests. So, this will work from one Snippet to another on the same exact page. But if you make an AJAX call or $modx->sendRedirect(), the placeholder is already gone.

      Order of operations is really important, as well. An uncached Snippet can get a placeholder from a cached Snippet, but not vice versa. Uncached already run after Cached. This gets a little weirder when you have uncached calls within uncached calls. I'm not even sure of the mechanics at this point. (Cached vs. Uncached addresses some really interesting issues that I don't have the expertise to even attempt to explain or address myself).

      With regard to Snippet Intercommunication, there are lots of ways to get that information to your Snippet. Since everything is based on the Resource, you could use an Output Modifier on the call and as a result run your Outputting Snippet. You could also run the 2nd snippet from within the 1st Snippet using $modx->runSnippet(). Another option is to have the 2nd Snippet use the 1st Snippet as a "check" condition. In this case, the 1st Snippet would not even be called by the Resource, but would get called when the 2nd Snippet is run. These are all viable options, as well.
        Website: Extended Dialog Development Blog: on Extended Dialog
        Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
        Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

        Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
        • 3749
        • 24,544 Posts
        Using runSnippet() is usually the best option, but you can also set a $_SESSION variable in one snippet and read it in another.

        In the first snippet:

        $_SESSION['myValue'] = 'something';


        In the second:

        $msg = $_SESSION['myValue'];




        ------------------------------------------------------------------------------------------
        PLEASE, PLEASE specify the version of MODX you are using.
        MODX info for everyone: http://bobsguides.com/modx.html
          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
          • 39932
          • 483 Posts
          +1 for the $_SESSION! With regard to using $_SESSION, you must remember that the value is preserved until the user logs out or the value is unset. You may also utilize $_REQUEST the same way. This will reset with every page request (unless the link sends it a value).

          Note: It is not advisable to transport data via $_GET or $_POST as these modify other variables as well.
            Website: Extended Dialog Development Blog: on Extended Dialog
            Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
            Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

            Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
            • 40553
            • 42 Posts
            Thank you for the comprehensive answer.
            Apparently both snippets are part of the same page yet I'm still unable to get the value of each placeholder. I will do deep research in caching, maybe that is the troublemaker in this case.

            When it goes to sessions, I thought about these but as Fuzzical Logic said, they preserve value.
            In my case the error message is a variable which changes troughout the snippet and may have different values, therefore stroing it in a session preserves the first value returned.

            Out of your suggestions I would most probably use the snippet inside snippet solution if I won't manage to get the placeholder value as I tried originally.
              • 39932
              • 483 Posts
              Perhaps $_REQUEST? It's a great alternative... Many people overlook it thinking that it is the same as $_GET or $_POST.
                Website: Extended Dialog Development Blog: on Extended Dialog
                Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
                Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

                Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
                • 40553
                • 42 Posts
                I would give $_REQUEST a try, yet I try to ommit passing data using GET or POST.
                This is really weird issue with the placeholders. I set one, I'm able to print its value using chunk, I'm able to print its value within the same snippet by using:

                $modx->getPlaceholder('message');


                But I can't get its value programatically in any other snippet even though none of the snippets on the page are cached.

                It looks like I'm stuck at the point where I can't move forward. Yet maybe there is another workaround?

                The thing is that the notification message saved as variable is set to a placeholder, then the placeholder [[+message]] is placed in a chunk which is at the end is outputed using snippet.
                What I want to achieve is to control the snippet which outputs chunk, so that it does not outup styled message chunk if the message (a placeholder from now on) is empty...
                  • 39932
                  • 483 Posts
                  $_REQUEST is not $_GET or $_POST. You can add to it without affecting $_GET or $_POST. $_REQUEST is both $_GET and $_POST and anything else you want. It's a catch all. And it is not persistent, so it won't save to the cookie, affect passed values, etc...

                  Further, any additions won't be added to the Header. I use this in order to pass values between plugins and it works extremely well. The only thing you ahve to worry about is if a kiddie chooses a field in either GET or POST that is named the same as your array key. Just make it obscure, and you should be fine.
                    Website: Extended Dialog Development Blog: on Extended Dialog
                    Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
                    Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

                    Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
                    • 40553
                    • 42 Posts
                    I see. But $_REQUEST is used to collect data sent with the use of forms with either GET or POST method and ideally I would like to be able to access a variable as if it was a global one without needing to submit a form holding the value.
                      • 39932
                      • 483 Posts
                      You can, absolutely. This is one of the misconceptions of $_REQUEST. $_REQUEST is used for any request parameter. That's why it is used dynamically by all CMSes. And that is what it is designed for... so that you can change the Request without changing the submission data, but still have access to it.

                      Simply, set $_REQUEST['paramName'] in one Snippet. Then in the next, get $_REQUEST['paramName']. None of your other $_GET or $_POST are affected. Additionally, it does not require a form. $_GET and $_POST require a form. $_REQUEST does not (but it still combines all forms if you have them)

                      Test: Set Request
                        $_REQUEST['joesnuffsbeetles'] = 'killme';
                      


                      Test: Get Request
                         $myVar = $_REQUEST['joesnuffsbeetles'];
                         echo $myVar;
                      


                      See? No forms required!! I use this all the time. [ed. note: fuzzicallogic last edited this post 14 years, 1 month ago.]
                        Website: Extended Dialog Development Blog: on Extended Dialog
                        Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
                        Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

                        Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".