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

    I’m looking for a way to change the template on the fly. I’m building serval sites witch need a jquery-colorbox to view general conditions for example. But in a colorbox view I don’t want to see the whole site design, I only want the text view.

    I was thinking to add a get parameter like ...html?popup=true and create a plugin to catch this and set the template temporary to another template id wich is empty. In this way all pages are viewable in the colorbox. The colorbox pages are somewhere in the menu of the site so the page couldn’t have the template set. But how he handles cache etc. I don’t know.

    Maybe one you have done this before or had a better idea?

    Regards, Bert
      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
      • 4172
      • 5,888 Posts
      I use this snippet.

      named it ’ajaxSwitch’
      <?php
      
      $output = '';
      
      $properties =& $scriptProperties;
      $properties['ajaxid'] = isset($_REQUEST['ajaxid']) ? $_REQUEST['ajaxid'] : 'none';
      $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'])) {
      
      if ($properties['ajaxid'] == 'none'){
          $output = $modx->getChunk($defaultChunk);      
      }else{
          $output = $modx->getChunk($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'];
      }
      
      
      return $output;


      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.





        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
      • Thanks for your code. Very usefull. Not exactly what I wanna do, but helpfull to create what I want.. If someone else has any other idea I will take a look at it
          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
          • 28120
          • 380 Posts
          I did this by adding the following as my only line in the template

          [[$[[!gallerySwitch]]]]

          this snippet checks to see if the get parameter exists to decide which chunk to call

          <?php
          if(isset($_GET['popup']) && $_GET['popup'] == 1) {
          	$template = 'galleryHighslide';
          } else {
          	$template = 'galleryStandardPage';
          }
          return $template;
          


          The chunk ’galleryStandardPage’ then cross references other chunks that are the site building blocks e.g.

          [[$header1]]
          [[$header2]]
          [[*content]]
          [[$footer1]]
          [[$footer2]]

          and the chunk ’galleryHighslide’ is just

          [[$header3]]
          [[*content]]
          [[$footer3]]
            • 4172
            • 5,888 Posts
            Quote from: sparkyhd at Jun 30, 2011, 06:37 AM

            I did this by adding the following as my only line in the template

            [[$[[!gallerySwitch]]]]

            this snippet checks to see if the get parameter exists to decide which chunk to call

            <?php
            if(isset($_GET['popup']) && $_GET['popup'] == 1) {
            	$template = 'galleryHighslide';
            } else {
            	$template = 'galleryStandardPage';
            }
            return $template;
            


            The chunk ’galleryStandardPage’ then cross references other chunks that are the site building blocks e.g.

            [[$header1]]
            [[$header2]]
            [[*content]]
            [[$footer1]]
            [[$footer2]]

            and the chunk ’galleryHighslide’ is just

            [[$header3]]
            [[*content]]
            [[$footer3]]

            you know, this is completly unchached, then.
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
            • Quote from: Bruno17 at Jun 30, 2011, 07:48 AM

              Quote from: sparkyhd at Jun 30, 2011, 06:37 AM

              I did this by adding the following as my only line in the template

              [[$[[!gallerySwitch]]]]

              this snippet checks to see if the get parameter exists to decide which chunk to call

              <?php
              if(isset($_GET['popup']) && $_GET['popup'] == 1) {
              	$template = 'galleryHighslide';
              } else {
              	$template = 'galleryStandardPage';
              }
              return $template;
              


              The chunk ’galleryStandardPage’ then cross references other chunks that are the site building blocks e.g.

              [[$header1]]
              [[$header2]]
              [[*content]]
              [[$footer1]]
              [[$footer2]]

              and the chunk ’galleryHighslide’ is just

              [[$header3]]
              [[*content]]
              [[$footer3]]

              you know, this is completly unchached, then.

              Thanks for this guys!

              Uncached.. Could this be cached also or do you need to write your own mechanism for that?
                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
                • 4172
                • 5,888 Posts
                this does the same as my snippet above, but my snippet can cache the results.

                you would use it this way in this case:

                [[!ajaxSwitch? 
                &ajaxChunk=`galleryHighslide` 
                &defaultChunk=`galleryStandardPage`
                &cache=`1`
                ]]


                send with your URL a &ajaxid=something
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                • Quote from: Bruno17 at Jun 30, 2011, 08:13 AM

                  this does the same as my snippet above, but my snippet can cache the results.

                  Great! I will check it as soon as I can!
                    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
                    • 28120
                    • 380 Posts
                    Hi Bruno

                    I wasn’t too worried about caching, I just needed a solution smiley

                    But you’re solution looks better if it achieves the same AND caches.

                    Not sure how your code decides to use ajaxChunk - how do I send it the parameter?

                    [[!ajaxSwitch? 
                    &ajaxChunk=`galleryHighslide` 
                    &defaultChunk=`galleryStandardPage`
                    &cache=`1`
                    ]]
                    
                      • 4172
                      • 5,888 Posts
                      Quote from: sparkyhd at Jun 30, 2011, 09:49 AM

                      Hi Bruno

                      I wasn’t too worried about caching, I just needed a solution smiley

                      But you’re solution looks better if it achieves the same AND caches.

                      Not sure how your code decides to use ajaxChunk - how do I send it the parameter?

                      [[!ajaxSwitch? 
                      &ajaxChunk=`galleryHighslide` 
                      &defaultChunk=`galleryStandardPage`
                      &cache=`1`
                      ]]
                      



                      just send with your ajax-request
                      &ajaxid=something


                      I use the ajaxid to get different parts from my ajax-chunk into the frontpage.
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!