We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38593
    • 129 Posts
    I have a snippet that fetches XML data from an external server and then creates four placeholder values, which are used in four different areas in the template.

    Currently the snippet is called one line before the first placeholder's position (about midway through the template code). Would there be any time advantage in the page rendering if the snippet was called at the start of the page?

    As any difference would be milliseconds, I don't have any way to measure/test my theory, so am hoping the knowledgeable can shed a little light.
      Pedalers Bicycle Tours my website forged with MODX
      vandergraaf-M static website generator for MODX
      • 4041
      • 788 Posts
      This example code will work for Evolution, I'm not sure if these functions are available in Revolution.
      Modify your snippet, then add the [+process_timing+] placeholder in your template.

      $xstart = $modx->getMicroTime();
      
      //
      // your snippet code
      //
      
      $xend = $modx->getMicroTime();
      $ptime = str_replace('-','',sprintf("%2.4f s", $xstart - $xend));
      
      // uncomment the following line to print the time where the snippet is placed
      // return $ptime;
      
      // create the timing placeholder
      $modx->setPlaceholder('process_timing',$ptime);
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 38593
        • 129 Posts
        Thanks, but I was wondering about MODX's overall page rendering speed, not about the processing speed of the snippet itself.

        I guess another way to ask the question ... as MODX is rendering a page, when it encounters a snippet call, does it stop page rendering, run the snippet and then resume page rendering. Or does it process the php separately while continuing to render the page.

        And a related (sort of) question, with the placeholders I am generating through the snippet, are they cached? The snippet is non-cached using the !, but do I also need to add the ! to each [[+placeholder]]? Or does non-caching the snippet make its outputted placeholders also non-cached?
          Pedalers Bicycle Tours my website forged with MODX
          vandergraaf-M static website generator for MODX
          • 32316
          • 387 Posts
          I guess another way to ask the question ... as MODX is rendering a page, when it encounters a snippet call, does it stop page rendering, run the snippet and then resume page rendering. Or does it process the php separately while continuing to render the page.
          I think the answer is neither.
          Think about it a snippet can do - for example it can add css or javascript to the head of a page - so I think you'll find that modx will run all the snippets before starting to render a page, there is no way for it to 'know' if everything is in place to start rendering until all snippets are evaluated.
          Uncached snippets are run after cached snippets if that makes any difference, and of course uncached snippets run every time the page is loaded - they become part of the cached page after the first page view.
          And a related (sort of) question, with the placeholders I am generating through the snippet, are they cached?
          What do you mean by placeholders?
          How are you creating these placeholders?

          Oh and which version of Modx are you using? - I only know revo, but I am pretty sure that caching is not the same in evo and revo - so answers to your questions may depend on the version you are using
            • 38593
            • 129 Posts
            So if I understand whistlemaker, MODX will look for all the snippets, run them and then render the page? So where the snippet appears in the template has no bearing on how fast MODX creates the page?

            Our snippet creates 4 different strings and then I use setPlaceholder to create the placeholder values. My apologies if I am not explaining it well, it is my first foray into MODX. An extremely simplistic version of the snippet is below:

            Fetch the XML file;
              foreach ($result...) {
                $createstring .= "<li>$xml_item</li>\n";
              }
              $modx->setPlaceholder('placeholdername', $createstring);


            Then within the template I add each of the placeholders using:

            [[+placeholdername]]


            I am using the latest version of Revolution.

              Pedalers Bicycle Tours my website forged with MODX
              vandergraaf-M static website generator for MODX
              • 32316
              • 387 Posts
              So if I understand whistlemaker, MODX will look for all the snippets, run them and then render the page?
              Yes - though I tend to think that browsers render pages and the php code 'creates' the page - running snippets is part of creating the page - the only difference between 'normal php' and modx snippets (which are just a way of embedding php into a page) is that cached snippets are run before uncached snippets (I think because the cache page contains the results of the cached snippets, and the uncached snippet tags intact - take a look at a cached page to see what I mean, core/cache/resource/web/resources/5.cache.php where 5 is the id of the cached page)

              So where the snippet appears in the template has no bearing on how fast MODX creates the page?
              Don't believe so - see below for a long winded explanation.


              oh placeholders - my brain didn't connect placeholders with modx placeholders, even though it pretty obvious!

              So I just did a little test for myself and you can put the snippet that sets the placeholder after the placeholder and it still works - which shows you that modx does not just work down the page it is parsing/evaluating the snippet before it replaces placeholders order on the page does not matter.

              If you call the snippet uncached then you will find the placeholder tag in the cache file, if you call the snippet cached you will find the placeholder tag replaced with the contents of the placeholder. So no need to call the placeholders uncached - the system is smart!

              MODX is evaluating the snippets before replacing placeholders (which is how it needs to work, this means you don't have to worry about getting the setPlaceholders all before the placeholders in the code, which makes it possible to add placeholders to your page without worrying about where the snippet is on the page) and likewise it can't replace the placeholders before it has figured out what they are so in your case modx can't really do anything until your xml back.
              So I think it does not really matter where you put this code - it will take however long it takes and hold things up while its doing its thing.

              If you want to speed things up - that is have the page created by modx and then sent to the browser to be rendered by the browser and not worry about the xml fetch blocking things perhaps you could use AJAX - have some javascript grab the data after you have sent some or all of the page to the browser and then insert the content where the placeholders would have been.


                • 28042 ☆ A M B ☆
                • 24,524 Posts
                If you are talking about Evo see this for a discussion of exactly what's going on in the parser. http://sottwell.com/modx-from-the-inside.html
                  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
                  • 38593
                  • 129 Posts
                  Thanks whistlemaker.

                  So all said and done, I suppose there could be a very small advantage to putting the snippet early, as I assume MODX will read through the template code / cached page to figure out which snippets need to be called, and the sooner it finds one, the sooner it can start processing the snippet?? Ultimately we would be talking only micro-seconds difference, but if a minor change in code sequence has a positive effect why not employ it smiley

                    Pedalers Bicycle Tours my website forged with MODX
                    vandergraaf-M static website generator for MODX