We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14883 ☆ A M B ☆
    • 450 Posts
    One of our sites is going to start offering its pages' content through an external portal site that will load them as an overlay iframe. In other words, they want to put links on the external portal that link to our site pages, but load them in an overlay window. They want to load versions of our pages that just include the page title and content. My thought is to write a script that looks for a query string value like "contentonly=true" and if it finds it, serves the simple version of the page. (I already have a snippet that runs at the start of all my templates, so I could easily add this check to that snippet). I would like to write code that (if contentonly=true):

    • loads a simple HTML structure (either from a template or a chunk; I'm not sure which would be better)
    • loads the values for *pagetitle and *content into those fields in the chunk/template (I think this just means parsing the template/chunk, right?)
    • Sends the results of the parsed simplified template/chunk as the value for the requested resource (rather than processing anything else in the "normal" calling of the resource

    Does this make sense? I want to write a test that looks for a given value in the query string, and if it finds it, delivers an alternative version of the resource's content and stops the normal processing of the resource.

    Has anyone done something similar? Any pointers or recommendations?
      • 18373 ☆ A M B ☆
      • 3,141 Posts
      You might like InstantAPI for that: http://modx.com/extras/package/instantapi

      It basically loads up the page as a JSON object instead of parse it through the template, which means that the external portal site could load the JSON, and get access to the individual fields and a parsed content blob.
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 3749
        • 24,544 Posts
        InstantAPI sounds good. I don't think your approach will work because you're still going to get the all the HTML code in the template, which will have to be there in order for the page to work normally.

        The only other thing I can think of is (if the $_GET variable is set) have your snippet set a $_SESSION variable with the page ID and forward the user to another page that has an empty template and no content other than a snippet tag for a snippet that reads the $_SESSION variable, gets the resource object, calls toArray() on it and uses the results to return a processed Tpl chunk. Something like this (untested):

        [[!CheckContentOnly]]


        <?php
        /* CheckContentOnly snippet */
        if(isset($_GET['contentonly] && ($_GET['contentonly'] == 'true'))) {
            $_SESSION['docId'] = $modx->resource->get('id');
            $url = 'http://url/of/content-only/page';
            $modx->sendRedirect($url);
        }
        return '';




        [[!ShowContentOnly]]



        <?php
        /* ShowContentOnly snippet */
        $output = 'Resource not found';
        $docId = $_SESSION['docId']'
        $doc = $modx->getObject('modResource', $docId);
        if ($doc) {
            $fields = $doc->toArray();
            $output = $modx->getChunk('MyTpl', $fields);
        }
        return $output;
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 4172
          • 5,888 Posts
          something like that?
          https://forums.modx.com/forums/thread/15204/change-template-on-the-fly#dis-post-82229

          the ajaxswitch - snippet could be much simpler and you can wrapp it with getCache then.
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 14883 ☆ A M B ☆
            • 450 Posts
            Actually, now that I've slept on it, I think with my current template paradigm, it will be quite easy. My templates look more-or-less like this:

            [[setTemplatePlaceholders?&outputChunk=`html_2_0`]]


            setTemplatePlaceholders does a bunch of stuff (mostly obvious by the title), and then returns the named chunk. It should be fairly trivial to test for the 'contentonly' value, and just return a different chunk containing the simple HTML structure.

            The way my templates are set up has some other problematic issues that need to be addressed (namely load times), but in this case it comes in pretty handy.
              • 14883 ☆ A M B ☆
              • 450 Posts
              I added this to the top of my 'setTemplatePlaceholders' snippet:

              if ($_SERVER['SERVER_NAME'] == 'content.mydomain.com') {
              	return $modx->getChunk('ContentOnly');
              }
              else {
               // all the existing code from the snippet
              }


              I decided to go with an alternate hostname/alias rather than the query string method. I also use the hostname in setting the base href value; this way if someone clicks on a link from a content-only page, they get the content-only version of the next page.

              I also had to change the setTemplatePlaceholders snippet call to run it uncached.

              It is working great now.

                • 3749
                • 24,544 Posts
                I'm glad you got it sorted. smiley
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting