We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    Demo: http://www.sottwell.com

    To keep a page’s template from being switched, place

    <!-- donotswitch -->


    in its assigned template. You can still use that template for switching other pages to, just pages originally assigned that template won’t be switched.

    On your page switching menu, have links passing the template ID in the query string:

    <a href="[~[*id*]~]&template=1">Original template</a>
    <a href="[~[*id*]~]&template=2">2-column template</a>
    <a href="[~[*id*]~]&template=3">1-column template</a>


    And here’s the plugin code:

    if(!strstr($modx->documentContent, "donotswitch")) {
    
    if (isset($_GET['template'])) {
      $overrideTemplate=$_GET['template'];}
    else if (isset($_COOKIE['template'])) {
      $overrideTemplate=$_COOKIE['template']; 
    }
    if (isset($overrideTemplate) && is_numeric($overrideTemplate)) {
      $modx->documentObject['template']=$overrideTemplate;
    
      $sql = "SELECT * FROM ".$this->getFullTableName("site_templates")." WHERE ".$modx->getFullTableName("site_templates").".id = '".$overrideTemplate."';";
    
      $result = $modx->dbQuery($sql);
      $rowCount = $this->recordCount($result);
      if($rowCount>1) {
        $this->messageQuit("Incorrect number of templates returned from database", $sql);
      } elseif($rowCount==1) {
        $row = $this->fetchRow($result);
        $modx->documentContent = $row['content'];
      }
      setcookie("template",$overrideTemplate,time()+604800, "/", "", 0);
    }
    
    }


    Set its System Events to be Template Service Events, OnLoadWebDocument.

    A cookie is set so the user’s selection is maintained across switchable pages in the site. If a user doesn’t have cookies enabled, he’ll get the one page switched, but the rest will not change. No harm done, it won’t have any ill effects on the default template.
      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
      • 21301
      • 93 Posts
      neverbeclever Reply #2, 21 years ago
      Nice extension! You might use the template=name version instead of template=id so you don’t have to poke into the database to get the template id.
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Actually, this is based on an existing plugin. I’m still a little fuzzy about what it’s doing.

        It looks like pages that you want to switch templates for can’t be cached. I’m also a little fuzzy on exactly what’s going on in the parser at this point. Hopefully I’ll figure out a way to override a cached page if it’s supposed to be using a different template.
          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
          • 21301
          • 93 Posts
          neverbeclever Reply #4, 21 years ago
          Quote from: sottwell at Oct 09, 2005, 04:17 PM

          Actually, this is based on an existing plugin. I’m still a little fuzzy about what it’s doing.
          I know, I’ve posted that plugin. The parser first checks the cache, if the cached page is used the ’OnLoadWebDocument’ will not be fired at all. And the plugin will not do it’s job.

          The ’OnWebPageInit’ event is called just before the cache-check and the ’OnLoadWebPageCache’ event when a cached page is used.

          I don’t see a simple way - other than turn caching off - to get this working when caching is enabled.
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            And I just discovered another serious limitation. You can’t have any TVs exclusive to any of the templates you switch to. Either a TV is attached to all the templates, or it can only be attached to the actual assigned template of the given page.

            I considered having trigger on two events, flushing the cache on OnWebPageInit, then doing the style switch on OnLoadWebDocument, but that sort of bypasses the purpose of caching in the first place.

            I suppose most sites wouldn’t use such a thing; a simple stylesheet switcher will be sufficient most of the time. And, the "donotswitch" comment can be inserted into the template of any document that needs a specific TV. So it still has its uses under certain circumstances; you just have to really want to use it for some reason, and modify your site accordingly.
              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
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Well, for what it’s worth, here’s the template switcher in action. Pages are not cached, and only the light blue thrashbox template uses a TV to select its sideboxes.

              http://www.sottwell.com
                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
                • 34162
                • 1 Posts
                Susan,

                Nice works!

                Can the switcher be a dropdown list with a thumbnail sample of the template?

                Thanks
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  It can be anything you want. I use a chunk:

                  <div class="switcher">
                  <ul>
                  <li><a href='[~[*id*]~]&template=8'>Original</a></li>
                  <li><a href='[~[*id*]~]&template=7'>Blue</a></li>
                  <li><a href='[~[*id*]~]&template=9'>Swan</a></li>
                  <li><a href='[~[*id*]~]&template=10'>Theo</a></li>
                  </ul>
                  </div>


                  Make it a dropdown, add images, whatever.
                    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
                    • 34162
                    • 1 Posts
                    I am thinking more in term of dynamic switcher that get the template info from the database rather than via a chunk which will require an edit everytime there is a change of the templates either add or delete.

                    I don’t recall in Modx there is a provision for storing a link to the thumbnail of the template for the purpose of previewing the template before do the switch. Is there?

                    Thanks
                      • 25663 MODX Staff
                      • 12,272 Posts
                      No preview thumbnails for templates.
                        Ryan Thrash, MODX Co-Founder
                        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me