We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14877
    • 110 Posts
    MODX Revolution 2.4.3-pl

    I happen to like storing all my PHP code in snippets when using MODX, so let's take that as a given.

    I have a chunk of PHP code that I want to "include" in certain snippets. It happens to involve establishing an xPDO connection to an external database and loading the package, but that is really irrelevant.

    Currently, I have the chunk of code in an external file (in .../assets/.../xxxx.php) and use in include_once(). This is because I don't know any way of creating an object in one snippet and then accessing it in another snippet. I'd be happy to just handle the code with some form of "include" or alternately, be able to pass variables to another snippet (without using $_SESSION which I do use).

    Any ideas?

    This question has been answered by BobRay. See the first response.

      __________________
      JRG
    • discuss.answer
      • 3749
      • 24,544 Posts
      It's not a good idea to use eval(), which makes it difficult to do what you want. One method is to write the chunk's content to a file, then include it, then unlink() the file when you're done.

      You can also just put the code in a snippet (created on-the-fly if necessary) and use $modx->runSnippet('SnippetName', $fields), where $fields is an associative array containing the snippet properties.

      If you need to create the snippet code on the fly, you can use $snippet->setContent($snippetCode);.


      If you do continue to use the file (and I don't really see why you wouldn't), I would put it under the core directory rather than the assets directory. I would also make it a class file and call the class methods in your other snippet. That way if it ever executes, it won't do anything.
        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
        • 14877
        • 110 Posts
        Quote from: BobRay at Apr 12, 2016, 12:26 PM
        It's not a good idea to use eval(), which makes it difficult to do what you want. One method is to write the chunk's content to a file, then include it, then unlink() the file when you're done.

        You can also just put the code in a snippet (created on-the-fly if necessary) and use $modx->runSnippet('SnippetName', $fields), where $fields is an associative array containing the snippet properties.

        If you need to create the snippet code on the fly, you can use $snippet->setContent($snippetCode);.


        If you do continue to use the file (and I don't really see why you wouldn't), I would put it under the core directory rather than the assets directory. I would also make it a class file and call the class methods in your other snippet. That way if it ever executes, it won't do anything.
        The piece of PHP code needs to be available on an on-going basis. Having it in a snippet which is read and written to a file, included and then unlinked is probably more code than in snippet.

        I'm going to take a look at runSnippet() and how it works. That sounds promising.

        Thanks Bob. If the runSnippet() approach works, I'll flag your answer.

        [Updated: 2016-04-13] That works great! The trick is that the PHP "return" statement can return anything, including an object. I had been worried that runSnippet() was somehow restricted to returning a string (the most common thing returned by a snippet). [ed. note: jrg last edited this post 10 years, 6 months ago.]
          __________________
          JRG
          • 3749
          • 24,544 Posts
          I'm glad that worked for you. I wasn't aware that a snippet called with runSnippet() could return an object. That's really interesting.
            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