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

    I’m curious if it’s possilble to load a page in a different template.

    In my case, I’m loading the weblogin snippet via Ajax.
    For this purpose, I need the template to be blank.
    As soon as i post the login, it redirects to a new page with a blank template.

    Is there a way to do this?
    e.g. index.php?id=15&template=4
    I could change the action part of the form using javascript, so this seems to be a reasonable solution

    Cheers!

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      &loginhomeid=`xx`, where xx is the ID of the page you want them to go to on login.
        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
        • 11913
        • 9 Posts
        Thanks!

        This almost does the trick.
        It just fails when someone enters a wrong password..

        For now, I should just let those who enter their password incorrectly deal with a nearly blank page...
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          There is a plugin for changing templates; it uses a query string like you mentioned. The page involved must be uncached, however, as when a page is cached the parser doesn’t even look for the resource’s template (unless the cache file for it doesn’t exist). So the plugin has to use an event that occurs before the parser checks for its cache status. It could delete the resource’s cache file if one exists, thus forcing the parser to load the template and re-parse the resource if the GET parameter exists.
            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
            • 11913
            • 9 Posts
            Hmm, I can see what you mean, but I can’t get it to work

            The plugin code I build fires at OnWebPagePrerender and is the following:
            if(isset($_POST['cmdweblogin']))
            {
            	$table = $modx->getFullTableName("site_templates");
            		$result = $modx->db->select("id, content",$table,"id='4' ");
            		if($modx->db->getRecordCount($result) == 1) {
            			$row = $modx->db->getRow($result);
            			$modx->documentObject['template']=$row['id'];
            			$modx->documentContent = $row['content'];
            		} else {
            			$this->messageQuit("Error retrieving template.");
            		}
            }

              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Just set the template ID; MODx will grab it and apply it to the document’s content. If you set the document’s content to be the template’s content, then all you’ll get is the template content plugged into the template where the [*content*] tags are. This could get really ugly really fast, as the [*content*] now evaluates to the template’s content, which has the [*content*] tag which now evaluates to the template’s content... tongue tongue
                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
                • 11913
                • 9 Posts
                I can see what you mean.
                The only problem is that this doesn’t work.

                I think it may be the event that triggers this code, could you advise me which event to use as the trigger?
                I’m currently using OnWebPagePrerender, and have stripped down my code to:
                if(isset($_POST['cmdweblogin']))
                {
                
                $modx->documentObject['template']=4;
                		
                }
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  By the time OnWebPagePrerender is invoked, the document is parsed and ready to send to the server.

                  http://www.sottwell.com/system-events.html

                  Here’s the code to my old template switcher, it may help.

                  // TemplateSwitcher - plugin for MODx
                  // [email protected]
                  // released to the Public Domain
                  // uses OnLoadWebDocument event
                  // stores template choice in cookie
                  // add the usecookie argument to the Configuration
                  // &usecookie=Use Cookie;list;no,yes;no
                  // default is 'no'
                  // set Configuration to 'yes' to use cookie
                  // place an HTML comment <!-- donotswitch --> in the content of pages you don't want switched
                  // add template=templatename to the URL
                  
                  if(!strstr($modx->documentContent, "donotswitch")) { // this page is not switchable
                      if (isset($_GET['template'])) {
                        $overrideTemplate = $_GET['template'];
                     } else if (isset($_COOKIE['template']) && $usecookie == 'yes') {
                        $overrideTemplate = $_COOKIE['template']; 
                      }
                      if (isset($overrideTemplate)) {
                        $table = $modx->getFullTableName("site_templates");
                        $result = $modx->db->select("id, content",$table,"templatename = '".$overrideTemplate."'");
                        if($modx->db->getRecordCount($result) == 1) {
                          $row = $modx->db->getRow($result);
                          $modx->documentObject['template']=$row['id'];
                          $modx->documentContent = $row['content'];
                        } else {
                          $this->messageQuit("Error retrieving template.");
                        }
                        if($usecookie == 'yes') {
                          setcookie("template",$overrideTemplate,time()+604800, "/", "", 0);
                        }
                      }
                  } // end if page is switchable
                  
                    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
                    • 11913
                    • 9 Posts
                    It works!

                    The problem seemed to be the event used to trigger, but also selecting the template by id.
                    If you’d replace the templatename by template id, it somehow doesn’t work..

                    I’m still not sure if this is the most clean solution, but he, it works smiley

                    Thanks!