We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22656
    • 121 Posts
    I know these will execute in top-down fashion for a page, ie
    [[snippet1]]
    [[snippet2]]

    If snippet1 has $a=2; snippet2 can access this variable.

    However, is there a way to have $a=2; set in snippet2 and still access this variable in snippet1?

    Basically I have a snippet responsible for rendering a search box, and a separate snippet rendering search results. I'd like to be able to pre-populate the searchbox with criteria based on search results. In order to do this, I need to have access to search results, ie snippet2 from snippet1. However, if my searchbox snippet is physically sitting before the search results snippet, I am unable to do this.

    I understand I could have merged both snippets into 1, but then using MODX framework properly is defeated, might as well go back to a single PHP page. Basically design of the template determines whether snippet1 or snippet2 will be executed first, and this is a problem. This leaves the ordering of execution firmly in hands of how the template itself is ordered.

    This problem could be overcome by introducing "execution ordering" variable of some kind, ie

    [[snippet1 &EXEC=`2`]]
    [[snippet2 &EXEC=`1`]]


    Does anything like this already exist? Would anyone be able to offer alternative views or solutions on this? [ed. note: papachumba last edited this post 14 years, 2 months ago.]
      • 22656
      • 121 Posts
      Ooopssie, I just realised you CANNOT access the same variable from snippet2, which had been set in snippet1. I guess back to sessions it is... sad
        • 22303 MODX Staff
        • 10,725 Posts
        Put them in the order they need to execute and have them put their output into placeholders which you can then position anywhere in the output, regardless of the execution order of the snippets.
          • 22656
          • 121 Posts
          hmmm, nope cannot do this. I am not talking about simple output data
          Basically there's a whole recordset (array) of search results produced in snippet2 (from custom dbase), which I need to access in snippet1 in order to be able to output what I want, after this raw data has been parsed...

          so my question really is this - how do I share complex data structures across snippets, ie multidimensional arrays?
            • 3749
            • 24,544 Posts
            You can either put them in $_SESSION variables, or run the second snippet from inside the first one with $modx->runSnippet() and pass them as part of the array passed in that call.


            ------------------------------------------------------------------------------------------
            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
              • 9207 ☆ A M B ☆
              • 2,475 Posts
              Or you can stash any variables in the global $modx object -- sessions will last across many requests, but the $modx object is scoped for a single request. E.g. $modx->my_recordset = $something; that would be accessible to any Snippet that runs after the variable is set.
                • 22656
                • 121 Posts
                Scoped across a single request sounds perfect, no point filling ram with loads of sessions if this is destroyed at the end of page request anyway. tks everettg & all!
                  • 22303 MODX Staff
                  • 10,725 Posts
                  This is what placeholders are for. Set a placeholder with your result set at the beginning of page execution. Then you can utilize the placeholder in as many additional Snippets on the page as you want. I've done this many times, since beginning to work with MODX and it works very well.