We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22980
    • 84 Posts
    Hello

    I need to render a chunk with php, using a template’s TVs. (This is going in an iframe.) A similar situation would be needed to supply a chunk through AJAX. What would be the best way to go about this. Presumably there are modx helper functions for chunk parsing..?

    Thanks!
      • 3749
      • 24,544 Posts
      It’s not clear what you want. What’s in the TVs and how will you use them in the snippet?

      You can send the content of the TVs to your chunk by using the TV tags as parameters in the snippet tag:

      [!SnippetName? &tv1=`[*Tv1Name*]` &tv2=`[*Tv2Name*]` !]


      If the TVs contain chunk names, you can get the content of the chunks like this:

      $chunk1 = $modx->getChunk($tv1);


      You can parse them yourself, or use $modx->parseChunk();

      Then return the 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
        • 22980
        • 84 Posts
        parseChunk looks like what I need, awesome! What I want to do is use all the TVs from a particular page (document/resource) in parsing this chunk. Is there a good way of doing that?

        Thanks
          • 3749
          • 24,544 Posts
          Quote from: pehrlich at Feb 22, 2010, 03:05 PM

          parseChunk looks like what I need, awesome! What I want to do is use all the TVs from a particular page (document/resource) in parsing this chunk. Is there a good way of doing that?

          Thanks

          The easiest way is to just put tags in the chunk for the TVs.

          [*tv1*] in the chunk will be replaced with the value of the current page’s tv1 Template Variable.

          You can hide empty ones, if necessary, with PHX (or do other transformations on them).
            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
            • 22980
            • 84 Posts
            but see, there is no current page. A custom .php script is parsing the chunk, and the args for replacement need to be supplied. (my bad for not explaining this clearly earlier!)

            I could just write a query that would return an associative array of TVs for the desired page (which I know through GET), but if MODx has a built in ’correct’ way of doing this, I should use that.
              • 3749
              • 24,544 Posts
              Ah, then you want this, or one of the related functions listed on this page: http://wiki.modxcms.com/index.php/API:getTemplateVar
                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
                • 22980
                • 84 Posts
                aha excellent. using getTemplateVarOutput, parseChunk, and $modx->db->connect() and $modx->getSettings(). Below is my code for rendering a chunk template via iframe or ajax with all the TVs of a document, for the benefit of future searchers.

                <?php
                define('MODX_MANAGER_PATH', 'manager/');//relative path for manager folder
                
                include MODX_MANAGER_PATH.'includes/config.inc.php';
                
                // Setup the MODx API
                define('MODX_API_MODE', true);
                // initiate a new document parser
                include_once(MODX_MANAGER_PATH.'/includes/document.parser.class.inc.php');
                
                $modx = new DocumentParser;
                
                //key to atually connect to the db:
                $modx->db->connect(); // provide the MODx DBAPI
                $modx->getSettings(); // provide the $modx->documentMap and user settings
                
                $offfice = mysql_real_escape_string($_GET['office']);
                $office_args = $modx->getTemplateVarOutput('*', $office);
                 
                $result = $modx->parseChunk('office_info', $office_args, '[*', '*]');
                
                ob_Start();
                include('iframe_header.html');
                $header = ob_get_contents();
                ob_clean();
                
                ob_Start();
                include('iframe_footer.html');
                $footer = ob_get_contents();
                ob_clean();
                
                echo $header.$result.$footer;
                ?>
                  • 3749
                  • 24,544 Posts
                  Thanks for sharing that. smiley

                  I’m sure people will find it useful.


                  FYI, if you were running the code in a snippet within MODx, you could replace "echo" with "return" at the end. It makes things a little cleaner.

                  Also, inside MODx, the $modx object will already be available and you are already connected to the DB so you wouldn’t have to include the class file and instantiate it or call connect().
                    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
                    • 22980
                    • 84 Posts
                    right but how would you connect to a snippet with a HTTP request (ie, AJAX)?
                      • 3749
                      • 24,544 Posts
                      Quote from: pehrlich at Feb 22, 2010, 08:27 PM

                      right but how would you connect to a snippet with a HTTP request (ie, AJAX)?

                      Put the snippet tag in the requested page? I’m probably misunderstanding you.
                        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