We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27843
    • 170 Posts
    Hi,

    more and more I lack feature which return only content if I do ajax request to the MODx.

    I want update only primary content in my layout through DOM and not refresh site if web browser support javascript.

    In MODx 0.9.6.3 i have created few simply core hacks, which provide this feature for me and which supports modx cache too. The feature will be active, when i do ajax request and when i pass through $_GET or $_POST param only_content=1, in this situation MODx return only content, it will be parsed or it will be returned from cache.

    So I want to ask you, do you plan this in Revolution ?


    if you have interest, here is the code for MODx 0.9.6.3 only for idea:
    I have added this two methods to the DocumentParser class in the document.parser.class.inc.php which helps detect ajax request:
    <br />    function getHeader($header) {<br />        if (empty($header)) {<br />          ;<br />        }<br /><br />        /* Pokusiť sa ich najpr získať z globálneho poľa $_SERVER. */<br />        $temp = ’HTTP_’ . strtoupper(str_replace(’-’, ’_’, $header));<br />        if (!empty($_SERVER[$temp])) {<br />            return $_SERVER[$temp];<br />        }<br /><br />        /* Toto vyzerá ako jediné riešenie ako získať Authorization header na Apache serveri. */<br />        if (function_exists(’apache_request_headers’)) {<br />            $headers = apache_request_headers();<br />            if (!empty($headers[$header])) {<br />                return $headers[$header];<br />            }<br />        }<br /><br />        return false;<br />    }<br /><br />    function isXmlHttpRequest()<br />    {<br />        return ($this->getHeader(’X_REQUESTED_WITH’) == ’XMLHttpRequest’);<br />    }<br />


    Next i have changed this lines in the file document.parser.class.inc.php like this:

    better will be if i paste patch:
    http://rapidshare.com/files/200107183/onlyContent.patch

    There are few simply changes.
      • 17499 ☆ A M B ☆
      • 872 Posts
      You could have used the index-ajax.php from Modx to do the same thing without hacking into the core.
      Look at how the ajaxsearch snippet works.
        • 27843
        • 170 Posts
        Quote from: lossendae at Feb 23, 2009, 02:39 AM

        You could have used the index-ajax.php from Modx to do the same thing without hacking into the core.
        Look at how the ajaxsearch snippet works.

        Hi,

        Isn’t true

        In the "full MODx API solution" the ajax request is setting up with : new Ajax("[~22~]",{

        In the "lightweight" solution: new Ajax(’index-ajax.php’, {postBody: pars, update: $(’card’) }).request();});

        In the first solution you set up your ajax request with index.php and you call the MODx parser method to build the answer. In the second solution you set your ajax request with index-ajax.php and you call only the initialisation of database Variables. So you need to initiate a new document parser to use the global $modx and populate the $modx->documentMap array with:

        In either I don’t get only content, the target of my effort is obtain only content without template of any document and this MODx parser doesn’t support now.
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: snop at Feb 23, 2009, 03:01 AM

          Hi,

          Isn’t true

          In the "full MODx API solution" the ajax request is setting up with : new Ajax("[~22~]",{

          In the "lightweight" solution: new Ajax(’index-ajax.php’, {postBody: pars, update: $(’card’) }).request();});

          In the first solution you set up your ajax request with index.php and you call the MODx parser method to build the answer. In the second solution you set your ajax request with index-ajax.php and you call only the initialisation of database Variables. So you need to initiate a new document parser to use the global $modx and populate the $modx->documentMap array with:

          In either I don’t get only content, the target of my effort is obtain only content without template of any document and this MODx parser doesn’t support now.
          I never use index-ajax.php or any other custom ajax processor. MODx delivers Ajax requests from documents just fine; you simply select no template and make sure the document returns only what you need. MODx definitely supports this now, and I’d never do it any other way.
            • 27843
            • 170 Posts
            Quote from: OpenGeek at Feb 23, 2009, 08:43 AM

            you simply select no template and make sure the document returns only what you need. MODx definitely supports this now, and I’d never do it any other way.

            hi,

            if i select no template, so MODx returns only content, this is the way how document parser works.

            But how can I achieve, so as only content on my site was updated ? Should I create duplicates of all docs with no template and through ajax request load this docs e.g. on the basis of the url = (this.href) + ’-ajax’ in menu links (somehow like this could look uris for ajax requests) ?

            e.g. i have this doc structure, i use to describe pagetitle - document alias:
            <br />Home - home<br />Blog - blog<br />Products - products<br />About Us - about-us<br />


            and for ajax request i have this docs, which have blank document template:
            <br />Home - home-ajax<br />Blog - blog-ajax<br />Products - products-ajax<br />About Us - about-us-ajax<br />


            So when i do request e.g. <a href = ’/blog’ onclick="uri = this.href + ’-ajax’; ajax(uri,....); return false;" />, so MODx will return docs with no template and links (<a />) could be generated by wayfinder without problems.

            Is this right, or do you have any other philosophy for this case ?
              • 22629
              • 194 Posts
              Like opengeek says, MODx already supports this now (it’s been working on my site since 0.9.6.)

              What you need is another page on your site that contains the content that you want to be placed on-screen.

              For example, on one of my sites I had a "comments" feature so people could write comments on some of my pages (like a blog.) When the user hit "Add Comment", MODx sent an Ajax request to a page which processed the comment (added to the databse) and sent back "thank you for submitting your comment..." This content then replaced the "Add Comment" form on the page.

              The page that handled the post was in a designated "Ajax" container in MODx, with no template (or a template that simply has [*content*] in it.)

              Thinking about, you could probably use the template switcher plugin to achieve the same effect with a single page if you’re worried about duplicating content.
                Andy Shellam | www.networkmail.eu | @Pandy06269 @NetworkMail

                modx Revolution 2.2.6
                Windows 2012 | IIS 8 | php 5.4.11 | MySQL 5.5.29

                Content-Managed Websites Built on MODX
                • 27843
                • 170 Posts
                Quote from: Pandy06269 at Feb 23, 2009, 01:30 PM

                Like opengeek says, MODx already supports this now (it’s been working on my site since 0.9.6.)

                What you need is another page on your site that contains the content that you want to be placed on-screen.

                For example, on one of my sites I had a "comments" feature so people could write comments on some of my pages (like a blog.) When the user hit "Add Comment", MODx sent an Ajax request to a page which processed the comment (added to the databse) and sent back "thank you for submitting your comment..." This content then replaced the "Add Comment" form on the page.

                The page that handled the post was in a designated "Ajax" container in MODx, with no template (or a template that simply has [*content*] in it.)

                this is not the same what i want, i know that MODx pareser return output normally if it’s ajax request and you can write your own logic, can query db and so on

                I don’t know how to achieve, so as if javascript is on so content is loaded through ajax request and MODx parser return only content and if not so sites are refreshed normally with template and all is cached

                Opengeek tell that MODx already support this and you too, so how can I do that ?

                So because I have hacked document.parser.inc.php, because i don’t know how should i do this.

                Quote from: Pandy06269 at Feb 23, 2009, 01:30 PM

                Thinking about, you could probably use the template switcher plugin to achieve the same effect with a single page if you’re worried about duplicating content.

                If I use TemplateSwitcher i can’t use cache and this is very big hadicap.
                  • 22629
                  • 194 Posts
                  The 2 options are thus (unless anyone else knows of any others):


                  • Use opengeek’s (and my) suggestion and all the parts of the page that need to be ajax-ified, have a separate page with no (or minimal) template that only returns the content
                  • Use my suggestion and have TemplateSwitcher dynamically change the template of page’s called via Ajax to te blank/minimal template

                  If you cannot use TemplateSwitcher for the caching issue (and it’s that big a hit on your system) then you’re stuck with creating duplicate pages.

                  Of course you could always put all the content on the page into a chunk, and have the 2 pages (Ajax and non-Ajax) call the chunk to get the content, thus eliminating the duplicate content issue.

                  From a personal point of view, I don’t see this a fit use of Ajax (updating entire pages of content.) Users will be unable to bookmark your pages - if they land on the blog (non-Ajax) page and navigate around, they’d still wind up with the blog URL in their address bar - confusing. The back button (possibly) won’t work. JavaScript doesn’t execute when it’s passed back in your Ajax method.

                  Heaps of problems... I think Ajax is best left to what it does best - background-loading, checking, and updating parts of pages.
                    Andy Shellam | www.networkmail.eu | @Pandy06269 @NetworkMail

                    modx Revolution 2.2.6
                    Windows 2012 | IIS 8 | php 5.4.11 | MySQL 5.5.29

                    Content-Managed Websites Built on MODX
                    • 27843
                    • 170 Posts
                    Quote from: Pandy06269 at Feb 23, 2009, 02:41 PM

                    The 2 options are thus (unless anyone else knows of any others):


                    • Use opengeek’s (and my) suggestion and all the parts of the page that need to be ajax-ified, have a separate page with no (or minimal) template that only returns the content
                    • Use my suggestion and have TemplateSwitcher dynamically change the template of page’s called via Ajax to te blank/minimal template

                    If you cannot use TemplateSwitcher for the caching issue (and it’s that big a hit on your system) then you’re stuck with creating duplicate pages.

                    Of course you could always put all the content on the page into a chunk, and have the 2 pages (Ajax and non-Ajax) call the chunk to get the content, thus eliminating the duplicate content issue.

                    hmm, and this is what i’m talking about, MODx doesn’t support this now and opengeek wrote that "I’d never do it any other way.", so only one solution is simply core hack, which have changed few lines in document.parser.inc.php file and i don’t have any duplicates, have full support of the cache and MODx parser return only content when i pass $_REQUEST[’only_content’] and request have header $_SERVER[’X_REQUESTED_WITH’] == ’XMLHttpRequest’ (ajax request)

                    Quote from: Pandy06269 at Feb 23, 2009, 02:41 PM

                    From a personal point of view, I don’t see this a fit use of Ajax (updating entire pages of content.) Users will be unable to bookmark your pages - if they land on the blog (non-Ajax) page and navigate around, they’d still wind up with the blog URL in their address bar - confusing. The back button (possibly) won’t work. JavaScript doesn’t execute when it’s passed back in your Ajax method.

                    Heaps of problems... I think Ajax is best left to what it does best - background-loading, checking, and updating parts of pages.

                    All problems which you described are solvable.

                    Users will be unable to bookmark your pages - isn’t problem e.g. for http://trac.nathanhammond.com/jssm/ or dojo.back, i have sites where this works great e.g. my last page which i have only imported to the modx www.pemit.ic.cz, all URIs you can bookmark.
                    The back button (possibly) won’t work - back and forward buttons works great
                    JavaScript doesn’t execute when it’s passed back in your Ajax method - isn’t problem re-execute javascript when i need

                    And loading of the pages is superfast
                      • 22629
                      • 194 Posts
                      I’d need some convincing I’m afraid. For example, why would you use another library (which is another download/processing by the client), hack the MODx core (which will be a nightmare when it comes to upgrades), sacrifice additional functionality in JavaScript (e.g. onload) all so you can use Ajax to do something it wasn’t designed to do (and maybe save a few ms of speed) ?

                      On modern Internet connections the difference between loading a page via Ajax, and navigating to another page is minimal, so I don’t see speed as an argument. But, like I said it’s a personal opinion and probably off-topic.

                      opengeek and I have given you perfectly good solutions that don’t involve hacking the core and tbh I can even think of another - have a snippet that’s called at the start of each page (i.e. in your template) which returns the contents of your header/footer/menu if the HTTP header you’re looking for isn’t provided in the request.

                      And hopefully when that site goes live it will be a LOT quicker than it is now - it’s taking over a minute to load each page, making the site look even worse because you wind up with a blank content area while it’s waiting for the Ajax response, and the browser looks like it’s doing nothing - another usability issue.
                        Andy Shellam | www.networkmail.eu | @Pandy06269 @NetworkMail

                        modx Revolution 2.2.6
                        Windows 2012 | IIS 8 | php 5.4.11 | MySQL 5.5.29

                        Content-Managed Websites Built on MODX