We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33453
    • 141 Posts
    From the documentation -


    Snippets can also be run inside of other snippets by using the MODx API:
    Usage:
    $modx->runSnippet(’snippetName’);

    If I do it exactly like that it works, however I need to pass a parameter in so I put

    $modx->runSnippet(’snippetName?&id=53’); (I also tried it with quotes around the "53")

    This breaks it, and the snippet is not called.

    Is it possible to pass a parameter and, if so, what is the correct way to do it?

    Thanks.

      • 25663 MODX Staff
      • 12,272 Posts
      That’s coming in a future (not 0.9.1) recursive parser that’s being tested in parallel with our next/imminent release.
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 33453
        • 141 Posts
        I can’t wait grin


        To understand recursion one must first understand recursion
          • 22303 MODX Staff
          • 10,725 Posts
          Actually, you can call a snippet from within another snippet using the API. You pass the parameters as an array, in the second argument to the runSnippet() function...

          $snippetName= 'MySnippetName';
          $snippetParams= array('id'=> 53);
          $modx->runSnippet($snippetName, $snippetParams);


          What Ryan is talking about in regards to the recursive parser is being able to do this in the content...

          [[MySnippetName?id=[[MySnippetThatGetsAnId]]]]
            • 32241
            • 1,495 Posts
            Quote from: OpenGeek at Dec 22, 2005, 04:02 PM

            Actually, you can call a snippet from within another snippet using the API. You pass the parameters as an array, in the second argument to the runSnippet() function...

            $snippetName= 'MySnippetName';
            $snippetParams= array('id'=> 53);
            $modx->runSnippet($snippetName, $snippetParams);


            That’s a new discovery from me.
            I thought we have to set the variables before that, and force the snippet that was called from other snippet to use global variables.

            Anyway, thanks for the great info.
              Wendy Novianto
              [font=Verdana]PT DJAMOER Technology Media
              [font=Verdana]Xituz Media
              • 33453
              • 141 Posts
              Thanks Jason,

              that’s what I was hoping to hear. I have been using a session variable temporarily.

              Forgive my ignorance but how do I retrieve the parameters at the called end?
                • 22303 MODX Staff
                • 10,725 Posts
                The parameters basically get extracted into the scope of the eval, so add a $ in front of the parameter name, and there you have it...

                for instance, the `id` parameter of a snippet would be available as $id
                  • 15914
                  • 145 Posts
                  The documentation needs to be updated with the ability to pass parameters to it.
                    Kunal Kapoor
                    Technical Director
                    Limesharp Internet Limited - Effective, Functional, Standards Compliant

                    IRC Nick: KingKoopa
                    Skype ID: KingKoopa16