We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20371
    • 58 Posts
    Hi, I'm wondering what might be the best way to replace the entire response body from within a snippet.

    Why?
    To summarise, my snippet returns javascript content, which in turn sends xhr requests for additional content. I'm thinking it would be tidier, if the xhr requests were sent to the same page. So the one snippet would be able to both render the original javascript content, and deal with xhr requests later.

    How?
    I figured I could just print a json response and then die(); but that seems like a dirty hack, so I've been looking through the API trying to find a better way.

    I've been stumbling through the API, but I'm a novice, and I don't seem to be able to find the info I need. Thus far I've found the $modx->response object, and I can see that it contains things like $modx->response->body etc, but writing directly to that property from within the snippet has no effect, I assume because that property is over written later on.

    So I guess I'm looking for a way for the snippet to set $modx->response->body and then cancer further processing and just send the response.

    Is this possible? is there a better way?

    Thanks in advance.

    This question has been answered by BobRay. See the first response.

      • 4172
      • 5,888 Posts
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
      • discuss.answer
        • 3749
        • 24,544 Posts
        You need to put your code in a plugin.

        Which event you connect it to depends on what whether you need the template or need to have any tags parsed. This is the basic form:

        $output = &$modx->resource->_output; // get a reference to the output
        $output = 'Whatever you want';


        OnLoadWebDocument — Fires after the requested resource is loaded, but before it is parsed (not sure if the output is available here).
        OnParseDocument — Fires just before the MODX tags are processed.
        OnWebPagePrerender — Fires just before a completed web page in the front end is sent to the browser, after all tags have been processed.


        Keep in mind that you can also inject JS into the page with $modx->regClientStartupScript() or one of it's siblings: http://rtfm.modx.com/revolution/2.x/developing-in-modx/other-development-resources/class-reference/modx/modx.regclientstartupscript
          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
          • 20371
          • 58 Posts
          Quote from: BobRay at Nov 28, 2013, 06:56 AM
          You need to put your code in a plugin.

          Thanks... this is exactly what I needed to hear. Thanks for your help.