We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28584
    • 27 Posts
    I would like to be able to change the value of a template variable in code.
    can anyone give me the syntax to address a template variable in a plugin.

    a bit of background:
    I would like to create a style sheet switcher.
    my idea is to create a template variable named styleSheetName (defaulted to site1.css) using this in the template for the link to a stylesheet e.g.

    <link rel="stylesheet" href="[(base_url)]assets/templates/default/[*styleSheetName*]" type="text/css" media="screen" />

    and then re work susan otwells template switcher plugin to update the value of the template variable based on $_GET or $_COOKIE value (instead of currently switching the whole template by accessing $modx->documentObject[’template’] and setting it to the ID of the template on the GET/COOKIE)

    Thanks for any help especially with the syntax to access the template var which is the piece of the puzzle I’m currently missing.

    P.S.
    If anyone can think of a tidier way to switch the CSS file used based on a link or form variable I’m happy to consider other options...
      • 7923
      • 4,213 Posts
      You could use for example the docmanager unofficial api to change the tv value, see it in the resource repository.. or simply look at the setTv function for an example


        "He can have a lollipop any time he wants to. That's what it means to be a programmer."
        • 28584
        • 27 Posts
        Thanks for those useful techniques both.

        I’ve decided to simplify my style switcher to be based around a simple snippet instead of the plugin approach.
        this is because the template switcher makes if difficult to have multiple templates across the site as the template being used is based on a cookie value and not the documents template.

        my snippet
        <?php
        /*
         *	A simple stylesheet switcher
         *	Author: Stewart Dinage 
         *      [email protected]
         *      version 0.1
        */
         
        $switchTo = "site1.css";
        if (isset($_GET['stylesheet'])) {
            $switchTo = $_GET['stylesheet'];
            setcookie("stylesheet",$switchTo,time()+604800, "/", "", 0);
        } 
        else if (isset($_COOKIE['stylesheet'])){
                $switchTo = $_COOKIE['stylesheet'];
            }
        return $switchTo;
        ?>


        this snippet is called in the template like so
        <link rel="stylesheet" href="[(base_url)]assets/templates/default/[[styleSwitcher]]" type="text/css" media="screen" />


        and allows a simple url value to be written to a cookie and change the stylesheet used.
        it could do with a bit more attention to allow the default stylesheet to be named at the time of snippet call but functionally does the job.

        if anyone else has any nifty stylesheet/template ideas for switchers let me know.
        I think they’re very useful for offering differently accessible versions of your website, we could maybe suggest the best method choosen as a core snippet as its such a useful feature?