We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26439
    • 104 Posts
    This is the first time I have used a plug-in, so I fully expect user error to be the cause of this one, but can someone tell me what I am doing wrong?

    My goal is to for the user to be able to switch templates when viewing my site, similar to "zen garden", although not as pretty.

    I have tried to implement sottwell’s TemplateSwitcher plug-in.


    // add template=templatename to the URL

    So I have added to the default index page.

    <a href="[~1~]?template=default">default</a><br />
    <a href="[~1~]?template=scraf01">text only</a>
    


    scraf01 being the name of a template with no css file.

    Clicking on "text only" gives http://www.scraf.nl/index.php?id=1&refurl=%2Findex.php%3Fid%3D1%3Ftemplate%3Dscraf01&err=1

    If I turn on friendly url’s I get
    http://www.scraf.nl/minimal-base.html?template=scraf01

    huh
    • If you don’t have friendly urls on, [~1~] will resolve to "index.php?id=1", so you have to use &template= instead.
        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
        • 26439
        • 104 Posts
        Ok, thanks, fixed that, but still not getting any template changes.

        http://www.scraf.nl ( http://www.scraf.nl/index.php?id=1&template=default )
        http://www.scraf.nl/index.php?id=1&template=scraf01

        ???
          • 26439
          • 104 Posts
          [FIXED] Page was getting cached, so the parser wasn’t looking at the templates. ( thanks to sottwell for looking into it for me )

          Possible fix for this in the pipeline.
            • 23054
            • 62 Posts
            Here is my templateswitcher code (in german), but I think you can reveal the main idea:

            $sideIid is the id of the current document
            $id is the id of the template

            Please note, that all variables must be defined in both templates.

            <?php
            
            $id = $_REQUEST['id'];
            $sideId = $_REQUEST['sideId'];
            
            if (!isset($id) || empty($id))
            {   echo 'Keine Auswahl getroffen';
                return;
            }
            
            $path = "../../";
            
            include_once $path . "manager/includes/config.inc.php";
            include_once $path . 'manager/includes/document.parser.class.inc.php';
            include_once $path . "manager/processors/cache_sync.class.processor.php";
            
            $modx = new DocumentParser();
            
            $table = $modx->getFullTableName('site_content');
            $temptable = $modx->getFullTableName('site_templates');
            
            $sql = 'update modx_site_content set template=' . $id;
            $modx->db->query($sql);
            
            $sync = new synccache();
            $sync->setCachepath($path . "assets/cache/");
            $sync->setReport(false);
            @$sync->emptyCache();
            
            if (isset($sideId))
            {   $modx->sendRedirect($path . "index.php?id=" . $sideId);
                return;
            }
            
            $displayName = $modx->db->getValue("select templatename from $temptable where id=$id");
            echo "Menü auf <b>$displayName</b> geändert.<br>"
                 . "<p>Bitte den Zurück-Button benutzen"
                 . "<p><b> gegebenfalls Seite neu laden (Taste F5)</b>";
            
            return;
            
            ?>