We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 727
    • 502 Posts
    Hi, I am calling the MODx (Evolution) API from outside MODx. I.e. in a simple PHP script. How can I generate a complete page from the hash array returned by getDocument? For example:

    $data = $modx->getDocument($id);
    $data["content"] = "<p>Foo</p>";
    // what goes here to convert $data to parsed html?
    echo $html;
    


    thanks, Andy
      • 5340
      • 1,624 Posts
      Have you tryed Var_sump or print_array to see the values returned by getDocument?
        • 727
        • 502 Posts
        Yes. The only HTML is the content field.

        Andy
          • 4172
          • 5,888 Posts
          there are several ways to get a parsed document.
          One of them which I use to get complete parsed documents for ajax-responds is

          $modx->sendRedirect($_POST['docurl']);

          or
          $url=$modx->makeUrl($id);
          $modx->sendRedirect($url);

          see also:
          http://wiki.modxcms.com/index.php/API:makeUrl
          http://wiki.modxcms.com/index.php/API:sendRedirect



            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 727
            • 502 Posts
            Thanks for replying. But I think I didn’t really explain what I am trying to do.

            I want to change the content of a document outside of MODx. If you look at the example I gave I am getting the details of a page and changing the content field. Now I want to assemble a completed, parsed page from that hash array, therefore using my new content.

            thanks, Andy
              • 4172
              • 5,888 Posts
              can you explain your complete situation and what is the reason for doing that. I’m sure there are better ways to get what you want.
              In your example I can see only you want to get the document-array with given ID. Then you want to change the content with "<p>Foo</p>";
              And parse this new content????
              But where comes the id from? What is a real new example-content? from where do you call this script and so on?
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 727
                • 502 Posts
                I have a complex external PHP application that I want to make look like my MODx site. However I don’t want to duplicate templates, make the menu static, etc. Instead I want to wrap my external PHP application in HTML generated by MODx. That way I can modify the templates, the sidebar, the menu etc. and it will automatically be updated for the external app.

                The MODx API can be accessed from any PHP script simply by including index.php. $modx is then available for use.

                Andy
                  • 4172
                  • 5,888 Posts
                  This is what you can do:
                  <?php
                  
                  define('MODX_API_MODE', true);
                  $path = dirname(__FILE__).'/';
                  include($path.'index.php');
                  $content='this is my new content';
                  //$GLOBALS['app_content']=$content;//if you want to replace the content-field with plugin use this
                  $modx->setPlaceholder('app_content',$content);//if you want replace a placeholder use this
                  ob_start();
                  $_REQUEST['id'] = $docid;
                  $modx->executeParser();
                  $html = ob_get_contents();
                  ob_end_clean();
                  echo $html;
                  
                  ?>


                  if you want to replace the content field you can do it with this plugin and onParseDocument checked:
                  if (isset($GLOBALS['app_content'])){
                  $output= $modx->documentOutput;
                  $modx->documentOutput = str_replace('[*content*]',$GLOBALS['app_content'], $output);
                  }


                  But why not running your application as a modx-snippet?
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 727
                    • 502 Posts
                    Thanks! That will help at lot. smiley

                    Running as snippets seems to cause issues. Almost like the snippet is running inside a function and therefore has problems accessing globals. I didn’t really get into it because I don’t want to start hacking MODx or my external app.

                    Andy
                      • 3749
                      • 24,544 Posts
                      Quote from: Andy at Jan 25, 2010, 01:33 PM

                      Hi, I am calling the MODx (Evolution) API from outside MODx. I.e. in a simple PHP script. How can I generate a complete page from the hash array returned by getDocument? For example:

                      $data = $modx->getDocument($id);
                      $data["content"] = "<p>Foo</p>";
                      // what goes here to convert $data to parsed html?
                      echo $html;
                      


                      thanks, Andy

                      Generating a complete page is pretty complicated. It involves getting the document template from the DB and then parsing all the tags in the template (and tags contained in things references by those tags, etc., etc.).

                      That said, you may find the functions you need in the document parser (although you may want to call getDocumentObject() rather than getDocument()).

                      manager/includes/document.parser.inc.php
                        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