We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28120
    • 380 Posts
    oops - didn’t see the $_REQUEST[’ajaxid’]

    Thanks
    • Quote from: Bruno17 at Jun 19, 2011, 01:55 PM

      I use this snippet.

      named it ’ajaxSwitch’
      <?php
      ...


      in my template i put something like that:

      [[!ajaxSwitch? 
      &ajaxChunk=`ajaxTemplate` 
      &defaultChunk=`baseTemplate`
      &cache=`1`
      ]]
      


      than I create two chunks, one for default and one for ajax-calls.
      On my ajax-chunk I can place different snippets, which return output for ajaxcalls with different ajaxids.
      This way I can call different cached parts of my page by ajax with &ajaxid=`sidebox` or &ajaxid=`slider` in the URL.


      I made a little changes and add it as a plugin hooked on "OnWebPagePrerender" to change the output.

      /**
       * Switches templates on the fly by triggering a get parameter
       */
      
      $properties =& $scriptProperties;
      $properties['ajaxid'] = isset($_REQUEST['ajaxid']) ? $_REQUEST['ajaxid'] : false;
      $properties['ajaxChunk'] = isset($_REQUEST['ajaxChunk']) ? $_REQUEST['ajaxChunk'] : 'ajaxChunk';
      
      if(!empty($properties['ajaxid'])) {
        
        $output = '';
        $properties['cache'] = isset($cache) ? (boolean) $cache : (boolean) $modx->getOption('cache_resource', $properties, false);
        $properties[xPDO::OPT_CACHE_KEY] = $modx->getOption('cache_resource_key', $properties, 'default');
        $properties[xPDO::OPT_CACHE_HANDLER] = $modx->getOption('cache_resource_handler', $properties, 'xPDOFileCache');
        $properties[xPDO::OPT_CACHE_EXPIRES] = (integer) $modx->getOption(xPDO::OPT_CACHE_EXPIRES, $properties, 0);
        
        if($properties['cache']) {
          $properties['cachePageKey'] = $modx->resource->getCacheKey() . '/' . $properties['ajaxid'] . '/' . md5(implode('', $modx->request->getParameters()));
          $properties['cacheOptions'] = array(
            xPDO::OPT_CACHE_KEY => $properties[xPDO::OPT_CACHE_KEY],
            xPDO::OPT_CACHE_HANDLER => $properties[xPDO::OPT_CACHE_HANDLER],
            xPDO::OPT_CACHE_EXPIRES => $properties[xPDO::OPT_CACHE_EXPIRES],
          );
        }
        
        $cached = false;
        if($properties['cache']) {
          if($modx->getCacheManager()) {
            $cached = $modx->cacheManager->get($properties['cachePageKey'], $properties['cacheOptions']);
          }
        }
        
        if(empty($cached) || !isset($cached['properties']) || !isset($cached['output'])) {
        
          $output = $modx->getChunk($properties['ajaxChunk'], $_REQUEST);  
          
          if($properties['cache'] && $modx->getCacheManager()) {
            $cached = array('properties' => $properties, 'output' => $output);
            $modx->cacheManager->set($properties['cachePageKey'], $cached, $properties[xPDO::OPT_CACHE_EXPIRES], $properties['cacheOptions']);
          }    
        
        }
        else {
            $properties = $cached['properties'];
            $output = $cached['output'];
        }
        
        echo $output;
        die();
      }


      This checks the ?ajaxid=x&ajaxChunk=chunkName in the URL.. so could every page being loaded as ajax or in colorbox frame!

      The only note I found; some snippet calls like formit will not be processed (??)
        MODX Ambassador (NL) & Professional MODX developer
        Follow me on Twitter | Visit my page on Facebook | View my code on Github | View my script posts
        MODX e-commerce solution SimpleCart
        • 34103
        • 47 Posts
        Hi Bruno,

        this is great I looking for something like this but with some extensions:
        - not only for chunks, for resources and snippets also: Enter IDs then resources will be used, enter snippet code to run snippet.
        - enter an ID-selector which will included (for resources)
        - PreHooks (for displaying loader animations or something else)

        would be a great snippet.
        • Changing the template on the fly is possible with the SwitchTemplate extra, too. Every output is cacheable and the settings could be done in a CMP. Hope it is a good solution for this.