We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40092
    • 265 Posts
    Hi Y'all,

    Currently i am experimenting with a collapsible div on some of my landing pages. Hidden on page load, pops up when the link is clicked. For the particular page, it means that it has to load more content in and it slows the page down.

    What i am doing now is i load the content in in chunks and based on the landing page it slects the correct chunk.

    Like so:

    <?php
    if ($landingpage == "page1"){
        print '{{chunk_for_page_1}}';
    }
    elseif($landingpage == "page2"){
           print '{{chunk_for_page_2}}'; 
    }
    //etc etc with the elseifs
    else{}
    


    Might there be a better practice to speed up the pageload?

    Robin
      • 16278
      • 928 Posts
      There's certainly better practice for inserting text from a chunk as the output of your snippet, using an API call rather than PHP print:
      <?php
      if ($landingpage == "page1"){
          $output = $modx->getChunk('chunk_for_page_1');
      }
      elseif($landingpage == "page2"){
          $output = $modx->getChunk('chunk_for_page_2');
      }
      //etc etc with the elseifs
      else{}
      
      return $output;
      


      ;) KP
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        And I do believe that a switch structure is faster than a bunch of if/else blocks.
        switch ($landingpage) {
            case "page1":
                $chunk = "chunk-for-page1";
                break;
            case "page2" :
                $chunk = "chunk-for-page2";
                break;
            case ...
          
            default:
                $chunk = "default-chunk";
                break;
        }
        return $modx->getChunk($chunk);
        

        Even simpler, if your chunks are named appropriately, you could just do
        return $modx->getChunk("chunk-for-" . $landingpage);

        Or, unless you are using the same block for more than one "landing" page, just use a TV.
          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
          • 40092
          • 265 Posts
          Thanks KP and Sotwell, i figured it out.

          The method you suggested Sottwel, isn't that just for REVO? However your last simpler solution gave me the idea to go.

          I now simply go with:
          <?php  echo "{{".$_GET['landingpage'] . "_info}}";?>
          


          That removes a whole bunch of unneeded code smiley

          Best Regards,
          Robin