We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21301
    • 93 Posts
    Thanks for the new events in TP3.2!

    Here’s a little plugin that allows you to modify the document template at runtime. I’ve no idea if this could be usefull to others but I post it anyway. It might help some to understand the power of plugins!

    I noticed that there’s a (quite lengthy) snippet for etomite that does the same thing (cut&paste the link):
    EDIT: The author (jaredc thanks!) of the snippet kindly pointed out (in this thread) that this snippet does actually something else. It’s some sort of the same but not at runtime.
    www.etomite.org/browsesnippets.html?int_snipid=38&lst_category=10

    This is what I use to render a page with a ’print this page’ template. I’ve added a bit of javascript to the template that fires the printpage dialog of your browser.

    See also this thread:
    http://modxcms.com/forums/index.php/topic,753.0.html

    /*
     *  Written by: Theo Ketsinga
     *  Created: 8/2005
     *  For: MODx cms (modxcms.com)
     *  Name: ChangeTemplate
     *  Description: Enables template modification at runtime by parameter
     *  Events: OnLoadWebDocument
     *
     *  Put this text into a new plugin and set the System Event
     *  'OnLoadWebDocument'. You can now override the template for a
     *  page by adding the parameter &template=<template_id> to a page.
     *  Look into the table modx_site_templates for the template id to
     *  use. You could also change the SQL query below a little.
     *  Change 'id' into 'templatename' and your parameter understands
     *  the name of the template. And you should then remove the line
     *  $modx->documentObject['template']=$overrideTemplate;
     *  Anyway, if there's a need for this I'll post a modified plugin.
     */
    
    if (isset($_REQUEST['template'])) {
      $overrideTemplate=$_REQUEST['template'];
      if (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'];
        }
      }
    }
    


    EDIT: Added ’is_numeric’ check.
      • 33337
      • 3,975 Posts
      Hi Theo,

      Thanks for the nice Plugin !

      Best regards,

      zi
        Zaigham R - MODX Professional | Skype | Email | Twitter

        Digging the interwebs for #MODX gems and bringing it to you. modx.link
        • 1764
        • 680 Posts
        Very nice theo. When I get some time I’ll use this for a handheld-friendly version of my site. It should work nicely.

        - Adam
          • 32963
          • 1,732 Posts
          Very cool indeed smiley

          You can even use this same method to change the CSS (or skin) of the website. See the $modx->regClientCSS() function.
            xWisdom
            www.xwisdomhtml.com
            The fear of the Lord is the beginning of wisdom:
            MODx Co-Founder - Create and do more with less.
            • 34162
            • 1 Posts
            May be this can turn into a template/skin chooser from the frontend?

            Just a thought!
              • 32963
              • 1,732 Posts
              Quote from: chanh at Aug 24, 2005, 09:26 AM

              May be this can turn into a template/skin chooser from the frontend?

              Just a thought!

              Sure can smiley You might also want to store the template id/css selection inside the $_SESSION object
                xWisdom
                www.xwisdomhtml.com
                The fear of the Lord is the beginning of wisdom:
                MODx Co-Founder - Create and do more with less.
                • 21301
                • 93 Posts
                Quote from: xwisdom at Aug 24, 2005, 09:23 AM

                You can even use this same method to change the CSS (or skin) of the website. See the $modx->regClientCSS() function.
                As Susan poined out it would be better to use CSS to change the skin indeed. And with all the new API functions popping up this is getting soo easy! Adding the lines below to the plugin works right away! It adds the CSS directly after the <head> tag. If I remember correcly the CSS already present will overrule this since it’s loaded later on. Though I’m not sure about this. It might be better to have the CSS included just before the </head> tag instead.

                May be this can turn into a template/skin chooser from the frontend?
                Great idea! That would be easily done in a snippet as well. You’ll have to decide on where to store the users preference. This is also an issue with other settings like language. Could we have some sort of mechanism to store user preferences in the API? Or is it there already? wink - I see xwisdom commented on this as well smiley -

                $path_to_css = "assets/site/"; // Should be a plugin parameter
                if (isset($_REQUEST['css'])) {
                  $overrideCSS=$_REQUEST['css'];
                  $modx->regClientCSS($path_to_css.$overrideCSS.".css");
                }
                
                  • 32963
                  • 1,732 Posts
                  Quote from: theo at Aug 24, 2005, 09:55 AM

                  It adds the CSS directly after the <head> tag. If I remember correcly the CSS already present will overrule this since it’s loaded later on. Though I’m not sure about this. It might be better to have the CSS included just before the </head> tag instead.

                  Hmmm,

                  IMO if this plugin is be used site wide then you can edit template to not use any stylesheets and use the plugin to install a default stylesheet. Or you can use some regx function to remove existing style sheets from the template.


                  Could we have some sort of mechanism to store user preferences in the API? Or is it there already?  wink - I see xwisdom commented on this as well smiley -

                  Well that’s a nice idea and we can look into that possibility.
                  But for now you can serialize your user settings and save them to disk or maybe use a plugin save and load user settings site-wide.

                  Use the OnLoadWebPage event to load the user or cookie settings into a $_USERSESSION array.

                  Here other snippet/plugins can access the $_USERSESSION array and store or retrieve user settings:

                  global $_USERSESSION;
                  $lang = $_USERSESSION[’site_language’];
                  $skin = $_USERSESSION[’site_skin’];

                  Use the OnWebPagePrerender event to save the $_USERSESSION array as cookies or save to a file.
                    xWisdom
                    www.xwisdomhtml.com
                    The fear of the Lord is the beginning of wisdom:
                    MODx Co-Founder - Create and do more with less.
                    • 21301
                    • 93 Posts
                    Quote from: xwisdom at Aug 24, 2005, 10:10 AM

                    IMO if this plugin is be used site wide then you can edit template to not use any stylesheets and use the plugin to install a default stylesheet. Or you can use some regx function to remove existing style sheets from the template.
                    That would of course be the better solution smiley


                    Here other snippet/plugins can access the $_USERSESSION array and store or retrieve user settings:
                    I like the _USERSESSION idea. It would be good to have some sort of standard way of doing for snippets/plugins. For now I’ll use your method as a de facto standard wink
                      • 13577
                      • 302 Posts
                      Quote from: theo at Aug 24, 2005, 07:15 AM
                      I noticed that there’s a (quite lengthy) snippet for etomite that does the same thing...
                      Aw come on... it’s not THAT long! wink

                      Key point tho- it doesn’t do the same thing. It changes the template for the page. NOT at run time but for good. It was designed to change a bunch of pages templates from template A to template B so you don’t have to do it one by one. It’s long because I built in some precautions- like showing a list of all the pages about to be changed. Anyway- just wanted to make that clear!
                        Standard Disclaimer
                        I could be totally wrong.