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

    is it possible, with a snippet, to dynamically change a ressource’s template if I know its ID ?

    Thanks !
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      It can be done with a plugin, but the page in question can’t be cached, since a cached page is served without even looking for the template.

      A better solution is to use a single flexible CSS-controlled template, and use a TV to specify the .css file to use for any given page. With CSS you can show/hide/resize/reposition any HTML element, such as sidebar sections/divs or blocks for slideshows.
        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
        • 43746
        • 10 Posts
        Thanks sottwell,

        My client doesn't want any sort of responsive design - they want to display a dedicated page for desktop / tablet / handset - but without changing the URL of the page. We were thinking of having a single template that does the device detection and then pulls in the appropriate device-specific page based on the visitor's device. That's where we're at, just trying to figure out how to do it. We were wondering if there is something similar to modX.getChunk - but for templates. Do you know of any feature like that?
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          I would use MIGx or even MIGXdb for this; each resource would have its standard fields with two MIGx TVs, one for the fields for each extra device. Use a plugin to determine the device and pull in the appropriate TV values. However, the TVs cannot be cached, so while most of the page would be cached, these dynamic sections would not.

          I'm considering the possibility of creating a custom resource class just for this purpose. It would have to be able to have as many cached versions as needed; three in this case, with a plugin to determine which cache file to load.
            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
            • 23018
            • 353 Posts
            You want to change the template layout for the frontend, right?

            Don't know if this is efficient, but try this:

            Separate Template specific things (forms, TVs, etc.) from your template layout.

            All your templates should look like this:

            [[$template_[[!templatepicker]]]]
            


            Then use a snippet to change the name of the included chunk:
            ---------------------------
            You can get a value from:
            * a tv (templatepicker ,see below)
            * a whitelisted list of get parameter values
            * or any other parameter you want to use.

            The important thing here is that the snippet returns a value that makes sense in regard for the template chunk you want to include.

            Example: templatepicker TV:
            ----------------------------
            Type: selectfield
            option-values: default||homepage||contact||whatever)
            default: default

            Create chunks with the templatelogic:

            $template_default
            $template_homepage
            $template_contact
            $template_whatever

            This should do it.

            pepebe

            Edit: In regard to a mobile, tablet or screen design, you have several options. Guess the type of device (for example with: http://mobiledetect.net/ or http://detectmobilebrowsers.com/), set a cookie and reload the page. Grab the cookie value and use it to include a different chunk. I see several problems with this approach, but they can me solved. [ed. note: pepebe last edited this post 13 years, 5 months ago.]
              Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
              • 13428 ☆ A M B ☆
              • 1,031 Posts
              Changing the template on the fly is now possible with the SwitchTemplate extra. Every output is cacheable.
                • 46886
                • 1,154 Posts
                Wow that looks pretty cool!