We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22851
    • 805 Posts
    Dear all.

    I’m running a website that automatically serves either handheld or web formatted content for each page. My current method for doing this works and allows me to serve multiple formats while only having one version of the actual content. However, the method I use also prevents any of it from being cached using MODx caching mechanisms. In practice both the handheld and web versions of my content are pretty static and so it is wasteful to rebuild them all from scratch each time. Also, as Google comes to download more of the pages on my site and do it more frequently, rebuilding many pages in a short period of time is putting a big load on my server. I’d be very grateful if anyone can think of a different way of doing this that would allow both the handheld and web versions of the content to be cached independently, preferably using MODx’s normal caching mechanisms.

    Here is how I do it currently.

    I have a php function that checks user-agent etc to determine which format should be served. Within each template I have a single un-cacheable snippet ( using this syntax [!...!] ) that takes two chunks as arguments, one containing the template to use for the web formatted version and one containing the template to use for the handheld formatted version. The snippet calls the php function to determine which format should be served each time a page is requested and then executes the appropriate chunk. Naturally, the chunks containing the templates for the web and handheld formats have snippets within them. Currently MODx design says that all snippets within these chunks must be written as if they are cacheable ( using this syntax [[...]] ), but none of them are actually cached. (See http://modxcms.com/forums/index.php/topic,21206.msg131030.html#msg131030.)

    Thank you,

    Paul

    By the way, the website it http://uklivinginfo.webhop.net/ if you want to play.
      YAMS: Yet Another Multilingual Solution for MODx
      YAMS Forums | Latest: YAMS 1.1.9 | YAMS Documentation
      Please consider donating if you appreciate the time and effort spent developing and supporting YAMS.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      I think this could be done with a plugin. It could save the handheld to a separate file, or let the normal caching continue for normal pages. Then before checking the cache, it would determine the type, and if it’s handheld, check the cache for the handheld cache file, or let the parser continue normally for normal files. That would mean it would have to listen for several events.
        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
        • 22851
        • 805 Posts
        Dear Sottwell

        Thanks very much for your reply. It’s much appreciated. I can’t say that I fully understood your response though! Fortunately, after a few hours mulling it over I managed to find quite a good and simple solution, and it involves a plugin like your solution suggested too. Here it is for anyone that’s interested:

        a) Use php code directly within the template to choose between the two template chunks. At this stage MODx will treat this PHP as if it is normal text and wont evaluate it. Therefore, the actual document created will contain both handheld and web content and it can all be cacheable in the normal MODx way since none of it is within a non-cacheable snippet. The template might look something like this...

        <?php
        if ( ! function_exists( 'is_handheld' ) )
        {
          function is_handheld()
          {
            // analysis of headers...
          }
        }
        
        if ( is_handheld() )
        {
        ?>
          <html ...>
          <!-- handheld template goes here and is subject to normal MODx caching -->
        
          </html>
        <?php
        }
        else
        {
        ?>
          <html ...>
          <!-- web template goes here and is subject to normal MODx caching -->
        
          </html>
        <?php
        }
        ?>
        


        b) The next step is to make sure that this residual php code gets evaluated after the document has been constructed or retrieved from the cache but before it gets served. When the php gets evaluated it will filter out the correct part of the (cached) document. To do this, we create a plugin called phpEvalDocument, say, set it to be the last plugin that is executed on the OnWebPagePrerender event, and put this code in it:

        ob_start();
        eval('?>' . $modx->documentOutput . '<?php ');
        $modx->documentOutput = ob_get_contents();
        ob_end_clean();
        


        This code evaluates the remaining php in the document, captures the evaluated php and reassigns the documentOutput.

        The good thing about this approach is that it is even possible to export and use static html versions of the website. Just disable the phpEvalDocument plugin temporarily while doing the export and the php will remain in the document. Then all you would need to do is to use the .htaccess file to tell the server to evaluate the php in the static file version of the document, since it wouldn’t normaly do this if the file had an html extension for example. Then you can have a partly-dynamic static website!

        Cheers,

        Paul
          YAMS: Yet Another Multilingual Solution for MODx
          YAMS Forums | Latest: YAMS 1.1.9 | YAMS Documentation
          Please consider donating if you appreciate the time and effort spent developing and supporting YAMS.