We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45227
    • 12 Posts
    I am trying to build a website that depends heavily on ajax using modx. My pages are loaded via ajax. So when a user clicks a link to a page I get the id of the resource and retrieve the content field ($resource->get('content') and I just echo it to the calling ajax method to display.

    this works fine if my page contains just html tags and text but I have issues whenever i use a modx template tag e.g. [[$xyz]] for a chunk. The tag comes out as is, it is not executed/resolved. i.e. not converted to html

    I know there must be a way modx filters these tags out and coverts them to html. Will be glad if someone can point me to how so I can use to resolve the tags before sending down to ajax to display the html.

    Please note that my php code is written in a file, not in the modx manager.

    Thanks for any help, my project is stalled till i get this working. [ed. note: swedge218 last edited this post 12 years, 11 months ago.]
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Use a resource with no template, just whatever you want to return to the AJAX request. Since the AJAX request is received by the server like any other HTTP request, it'll invoke MODx and the processed content of the resource will be returned.
        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
        • 45227
        • 12 Posts
        Quote from: sottwell at Oct 21, 2013, 09:12 AM
        Use a resource with no template, just whatever you want to return to the AJAX request. Since the AJAX request is received by the server like any other HTTP request, it'll invoke MODx and the processed content of the resource will be returned.

        thanks @sottwell, for good measure i tried ur suggestion but it still doesnt work.

        I am using ajax get method to load ONLY the content of the resources, not the whole page into an area on a single page. I get to grab the resource content only and send it down to ajax success function for rendering. What I would like to know is how modx will convert a tag, a chunk say, [[$xyz]] to its equivalent html before displaying. Maybe I can do the same before sending to ajax.

        Thanks
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Do you want the whole page displayed elsewhere?
            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
            • 45227
            • 12 Posts
            Quote from: sottwell at Oct 21, 2013, 09:45 AM
            Do you want the whole page displayed elsewhere?

            No no I dont. I managed to do a sketchy upload. Please check at http://lda.techieplanetltd.com.

            I have a base template that displays the pages. On home, u see a video and a side nav for the site, click on INSPIRATION, u see the page fade out,then wait for the inspiration page which comes out without page refresh. I have only used ajax to load the content ONLY of inspiration resource into the div that held the video. But u get to see chunk calls displayed instead of their contents(html).

            I would like to be able to resolve the chunks(and other tags) before sending the content as my ajax expects just plain html and will render the markup. I believe this is exactly what modx does before rendering pages, if only I knew how or the right file to check the code up bt I am jst a newbie learning modx in face of deadlines.

            Thanks and pls I would clarify further if need be.
              • 4172
              • 5,888 Posts
              As Susan said, the easiest way to create a controller for the frontend is to create a Resource with an empty template.

              On that Resource you can place a snippet to get the rendered content of any resource.

              the snippet in its simplest form could look like that:

              <?php
              
              $id = $modx->getOption('res_id',$_REQUEST,0);
              $tpl = $modx->getOption('tpl',$scriptProperties,'');
              
              if ($object = $modx->getObejct('modResource',$id)){
                  return $modx->getChunk($tpl,$object->toArray());
              }
              


              On your connector-resource you would just habe that snippet-tag:

              [[!getRequestedResource? &tpl=`myResourceTpl`]]

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

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                What would happen if you used a symlink with no template?
                  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
                  • 45227
                  • 12 Posts
                  @Bruno17, thanks and i already was using a variant of that. I had a php file containing my snippet code. It is this file that my ajax calls and I used a file because I dont know how to reference a modx resource/snippet from an ajax call. I linked a js file containing the ajax call in my template head tag. My short hand get call here below

                  $.get('how do I reference a resource/snippet here',{id:id},function(data){


                  in my js file I actually put the relative url of the file i.e. assets/snippets/file.php. Ok thats my variant. Moving on...

                  I took some advice from your reply. I created a chunk with just [[+content]] placeholder and since I was already getting the resource object before, I did

                  echo $modx->getChunk('myTpl',$resource->toArray());


                  This worked fine some. Now chunks and snippets are loaded but TVs are not loaded. That is another issue as I will need TVs heavily on the site.

                  So my question now is how to get the TVs too to load or if your approach will solve that, how do I reference s resource/snippet from an ajax call.

                  Thanks
                    • 45227
                    • 12 Posts
                    A QUICK ONE
                    @Bruno17, thanks and i already was using a variant of that. I had a php file containing my snippet code. It is this file that my ajax calls and I used a file because I dont know how to reference a modx resource/snippet from an ajax call. I linked a js file containing the ajax call in my template head tag. My short hand get call here below

                    $.get(url,{id:id},function(data){


                    in my js file url is a var containing i.e. assets/snippets/mycodefile.php. How do I make this a reference to a resource/snippet instead?

                    WHAT I HAVE NOW

                    I took some advice from your reply. I created a chunk with just [[+content]] placeholder and since I was already getting the resource object before, I did

                    echo $modx->getChunk('myTpl',$resource->toArray());


                    This worked fine some. Now chunks and snippets are loaded but TVs are not loaded. That is another issue as I will need TVs heavily on the site.

                    So my question now is how to get the TVs too to load or if your approach will solve that, how do I reference a resource/snippet from an ajax call?

                    Thanks
                      • 45227
                      • 12 Posts
                      @Susan is a symlink not for loading one resource at a time. I need this for several pages on the site (if not all). I cant create a symlink for a every page as u know.

                      But if I know how to reference the symlink/resource from ajax call i could try to use all your suggestions as above.

                      However, I have a level of success as in my last reply...only TVs left now.

                      Thanks