We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10152
    • 156 Posts
    hello, is it possible to switch template before rendering a document whether it is called via ajax or not ?

    Ajax templates do not include header + footer but normal templates do, so the rendering would be identical with javascript or without.

    EDIT : i’m attempting to write a snippet for that, hum does anyone know what to put in the if condition ?
    
    <?php
    
         /*
         *      displays a chunk or not wheter the modx front-end document is called via ajax or not
         *
         *	PARAMS
         *
         * 		&chunk=`chunkname` 
        */
    
    
        if ( $chunk != '' ) { // how to know if ajax ?
    
            $output = $modx->getChunk($chunk);
    
        }
    
        echo $output;
    
    
    ?>
    
      • 22295
      • 153 Posts
      As always, there’s a couple of methods, where using ’HTTP_X_REQUESTED_WITH’ is the most common on server side.
      one example is here: http://www.electrictoolbox.com/how-to-tell-ajax-request-php/

      you could also do it only client side - request the full page normally and only use the element you want, for example: $(’#resultContainer’).load(’page.html #content’);

        • 28042 ☆ A M B ☆
        • 24,524 Posts
        If you use a MODx resource (with a ’blank’ template and only a snippet call in its content) as the AJAX back-end processor, you have all of the MODx API to hand. You can pass the ID of the page being viewed, as well as any other relevant data, through the AJAX call.
          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