We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42552
    • 6 Posts
    I have a snippet (sn_get_gallery_array) that creates a php array of information about my Gallery photo albums. The resulting array is quite small, two subarrays each with about 30 strings, and is returned at the the end of the snippet using
    return $output;
    .

    In another snippet (sn_galleries) I have
    $gals = $modx->runSnippet('sn_get_gallery_array');
    to get the returned array, but it always returns an empty array.

    I know the array in sn_get_gallery_array is not empty - within the sn_get_gallery_array snippet I can dump the array into a log file and see the array elements, and I can copy the array into a $_SESSION variable and later access them from there.

    So why does runSnippet() return an empty array? I don't have problems returning strings via runSnippet(), so I know it's not a syntax issue. I manually refreshed the cache a few times but that didn't make any difference. There are no errors either in ModX or in my server logs. Any ideas about what's wrong here?

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

    • discuss.answer
      • 3749
      • 24,544 Posts
      runSnippet() always returns a string. Try using this in the snippet being called with runSnippet():

      return $modx->toJSON($myArray);



      And in the snippet that calls it:


      $resultString = $modx->runSnippet('MySnippet', $params);
      $result = $modx->fromJSON($resultString);
        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
        • 42552
        • 6 Posts
        Quote from: BobRay at Jan 22, 2013, 04:08 PM
        runSnippet() always returns a string.

        Thankyou, that explains it.

        I'm new to ModX and I didn't realise runSnippet() had that limitation.
          • 3749
          • 24,544 Posts
          I'm not new to MODX, but it was a long time before I learned that. wink
            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