We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Quote from: paulb at Aug 19, 2008, 12:10 AM

    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?
    That’s exactly what it boils down to. The first layer that is parsed, if it is cacheable, will be cached into the document object and stored on the file system on first access. The next visit to the page will simply load the cached content stored for each top level cacheable tag. To always have content under mySnippet be dynamically determined, you have to make it non-cachable, i.e. [!mySnippet!]. Without it, the results of the snippet evaluated by the first visit will always be presented and evaluation of your snippet will not happen again until the cache is cleared.
      • 22797
      • 134 Posts
      That’s exactly what it boils down to. The first layer that is parsed, if it is cacheable, will be cached into the document object and stored on the file system on first access. The next visit to the page will simply load the cached content stored for each top level cacheable tag. To always have content under mySnippet be dynamically determined, you have to make it non-cachable, i.e. [!mySnippet!]. Without it, the results of the snippet evaluated by the first visit will always be presented and evaluation of your snippet will not happen again until the cache is cleared.

      That’s good to know. At the same time, since it is possible to get around this limitation by calling a chunk which in turn calls an uncached snippet, it seems that it would be possible to do this more directly, as I mentioned in an earlier post, by simply specifying that you want a certain snippet to come through uncached.

      The reason why I’ve set things up the way I have is because the main snippet is my entire template. In my template, I have this code and nothing else: [[template2008]]. I did this because there are a lot of different conditions that I need to satisfy under different circumstances, and I wanted to bundle them all together in PHP code, to make things more streamlined. So in the "template2008" snippet, I have a call to another snippet that I want to be uncached.

      My setup may not be typical, but the desire to call an uncached snippet under these circumstances doesn’t seem unreasonable to me.

      Maybe someone can offer a better way of setting things up? I want to keep the entire template as PHP code. Using PHx was my original solution, but it got to be unwieldy and impractical due to the complexity of the conditions, so I took it all out into PHP. Again, if there’s a better way, I’d love to hear it. (I’ll take the suggestion of not using Ditto for this circumstance to heart, but I’m also interested in the broader issue of being able to call uncached content from within cached content.)
        • 3749
        • 24,544 Posts
        It looks like you’re just selecting a random image. If so, MaxiGallery might be a better choice, although you’d still have some overhead compared to a dedicated snippet.

        I have a site where this is in a chunk. I just put {{RandomImage}} where I want it to appear. Maxigallery also makes it a lot easier to maintain your images. My galleryPictureTpl makes the picture a link to a slideshow of the whole gallery, which you probably don’t want.

        [[MaxiGallery? &gal_query_ids=`all` &query_level_limit=`5` &order_by=`random` &limit=`1` &pics_per_row=`1` &max_thumb_size=`110`  &thumb_use_dropshadow=`1` &galleryPictureTpl=`mgPicture`]]
          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
          • 35176
          • 193 Posts
          it’s totally reasonable. modx provide the ! syntax to do just that, to force a snippet tag to execute the UNcached snippet-- even if called from inside a cached page.

          The !bang modifier should therefor work with the runSnippet too, or some other method.
            jealousy<>love
          • The logic is obvious. The runSnippet function is called within a snippet. It is a function, not a snippet (the fact that it uses PHP code from a snippet is irrelevant). The snippet is cached. Therefor the output of said snippet, including the output of the internal runSnippet call, will be cached.

            How could the output of the runSnippet function not be part of the returned value of the snippet?
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 35176
              • 193 Posts
              By that logic, then if the parent snippet is called UNcached, then so should the inner snippet. Right?
                jealousy<>love
              • Indeed; the runSnippet function will be called every time the uncached snippet is evaluated - or a cached snippet for that matter; in which case it will only be run the first time the page is requested, as the snippet will only be evaluated the first time the page is requested.

                It helps if you stop thinking of the runSnippet function as a snippet. It does fetch the specified snippet’s code to evaluate, but it’s not a snippet. It’s a function. Like any user-defined PHP function. Its parameters are the snippet name whose code to fetch, and an array of any arguments to provide for the code’s use.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 35176
                  • 193 Posts
                  Ah. In that case, it cannot be a stand-in for an include.

                  So how do you do php includes?
                    jealousy<>love
                  • With the include directive. A snippet is not a .php file. If you want to run snippet code from the database, use runSnippet. If you want to include a file, use include just as you would in any PHP code.
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 35176
                      • 193 Posts
                      How do I get a php file into MODx?
                        jealousy<>love