We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22797
    • 134 Posts
    As far as I can tell, there isn’t a way to run the runSnippet() API function except in cached mode, is this true? It would be nice to be able to do something like this:
    $dittoOutput = $modx->runSnippet("Ditto", array(parents=>'12','tpl' =>'default'),0);

    Where the "0" at the end could mean "not cached." In principle, the function could be altered along these lines:

    function runSnippet($snippetName, $params=array(), cached=1) { ... etc ... }


    (refer to the wiki entry on the runSnippet API function: http://wiki.modxcms.com/index.php/API:runSnippet) I haven’t gotten far enough into the nuts and bolts of the caching mechanism to know how to accomplish that though.
      • 22797
      • 134 Posts
      As a workaround, I ended up creating a chunk with a call to an uncached snippet, but that’s an extra step that ought to be unnecessary. Essentially I did this in the snippet:
      $chunk = $modx->getChunk("my_chunk");
      	 echo = $chunk;

      And the chunk had something along these lines:
      [!Ditto? &parents=`12` &tpl=`default`!]
        • 10449
        • 956 Posts
        I’ve used runSnippet() several times, always uncached. o_O
          • 7923
          • 4,213 Posts
          What do you mean? as far as I know, it’s just the opposite, i.e. runSnippet always runs (eval with passed parameters) the snippet in "not cached mode".

          If you mean that cache code what you see in the wiki, it checks if the snippet code is found on cache already, if it’s not, the code is retrieved from db. But even if the code itself is found from cache, it doesn’t mean that the snippet runs in cached mode. The code is then run in eval in both options and with the passed parameters. Then the output from the eval is returned, i.e. the snippet output is not retrieved from cache.


            "He can have a lollipop any time he wants to. That's what it means to be a programmer."
            • 22797
            • 134 Posts
            The snippet that I was running from runSnippet() was Ditto. I was using it to pull in some random content (&randomize=`1`) for a web page from among several possibilities. But the same content would always load. MODx had cached the result of the snippet when it was run the first time. It would not change the content until the cache was cleared.

            The page had "Cacheable" checked (turned on). Now, I could uncheck that box, but that slows the page WAY down. I want only the snippet to be uncached. It seems that the call to the snippet using runSnippet() bypasses the condition that lets me choose whether to cache the result of the snippet as part of the page or to always run the snippet even though the rest of the page is cached.
              • 22797
              • 134 Posts
              Here is an excerpt of the actual code I used:

              if($doc['banner_images'] != '') {
                   $top .= $modx->runSnippet('Ditto',
                        array('documents' => $doc['banner_images'], 
                        'tpl' => 'ditto.content', 
                        'display' => '1', 
                        'randomize' => '1', 
                        'orderby' => 'pagetitle ASC')
                        );
              }
              else {
                     $top .= '<div id="no_main_content_header"></div>';
              }


              Obviously there is more going on elsewhere in the code, but this is the part that matters in this situation. $doc[’banner_images’] is a template variable that has the MODx id(s) of certain documents (comma-separated list) with some content (an image within a paragraph tag in this example).
              • Am I to understand that you are using Ditto to insert random header content in dynamic pages? That really is not an efficient solution IMHO, unless you are caching the content. A component meant to do something like this directly would be much more prudent and less resource intensive. Ditto is a very flexible snippet, and you can accomplish a lot of things this way, but that does not mean it is the best way to do it or that there is not going to be a huge overhead for using a generalized snippet for a specific requirement.
                  • 22797
                  • 134 Posts
                  That’s fair enough. Even so, the issue of not being able to run the snippet uncached remains.
                  • Quote from: paulb at Aug 18, 2008, 11:36 PM

                    That’s fair enough. Even so, the issue of not being able to run the snippet uncached remains.
                    If the snippet you are calling is embedded in content that is cached, then that is the only way this would not be possible. the runSnippet() function does not have the ability to return cached content.
                      • 22797
                      • 134 Posts
                      The runSnippet() function is being called from a snippet that is called from inside of a template. The template contains a line more or less like this:

                      [[mySnippet]]

                      and "mySnippet" contains the call to the runSnippet() function. As you can see, the call to the snippet above is cached, which is actually what I want, but I guess I can’t put a reference to an uncached item within a cached item. Is that what it boils down to?