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

    Create a document with this snippet call:

    [[MemTest]] (cached or uncached -- doesn't seem to matter)


    Create a chunk called "TestChunk" and put some plain text in it.

    Create a snippet called MemTest and put this code in it:

    $chunk = $modx->getObject('modChunk',array(
        'name' => 'TestChunk'
        ));
    
    print_r($chunk);


    "Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 13788775 bytes) in C:\xampp\htdocs\MODx097\core\model\modx\modparser.class.php on line 199"

    The results of getObject, used like this (code pasted from Shaun’s tutorial), seem to have a recursive personality.
    echo $chunk
    produces nothing.

    While messing with this, I did get some output for a while and saw the whole array of info on the current document followed by its fullly parsed content go by over and over.

    The correct answer is:

    echo $chunk->snippet


    but it’s not very obvious or intuitive.

    To paraphase part of Shaun’s tutorial page:

    Evolution:

    $chunk = getChunk('TestChunk');
    echo $chunk;


    Not anymore. Things are much simpler . . .

    Revolution:

    $chunk = $modx->getObject('modChunk',array(
        'name' => 'TestChunk'
        ));
    
    echo $chunk->snippet;


    I’m sure the new way is more efficient, flexible, powerful, and faster, but no way it’s simpler.

    I miss getChunk. wink

    Could we maybe bring it back as a wrapper?
      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
      • 28215
      • 4,149 Posts
      Don’t just straight print_r an xPDOObject - do this:

      print_r($chunk->toArray());
      
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 3749
        • 24,544 Posts
        Thanks! That will definitely save some time.

        print_r() seems to either execute the code it finds in the object or else it can’t tell where the object ends. Both seem odd.


          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
          • 28215
          • 4,149 Posts
          Quote from: BobRay at Oct 05, 2008, 10:16 PM

          print_r() seems to either execute the code it finds in the object or else it can’t tell where the object ends. Both seem odd.

          I think it’s because it’s printing out the entire xPDO object ($object->xpdo), which is a huge object. That’s probably more than the script can handle.
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: BobRay at Oct 05, 2008, 05:39 PM

            While messing with this, I did get some output for a while and saw the whole array of info on the current document followed by its fullly parsed content go by over and over.

            The correct answer is:

            echo $chunk->snippet

            Actually wrong; that is not the way to access the chunk output...in fact, you should never reference the snippet property except from the manager code for presenting and editing it in a form. This property will be subject to change in future versions; it remains the same in the initial release only to support legacy code with SQL queries that depend on the table/column names. This will give people time to migrate their code to the Revolution way of doing things while their old code isn’t horribly broken in the meantime.
            Quote from: BobRay at Oct 05, 2008, 05:39 PM


            but it’s not very obvious or intuitive.

            To paraphase part of Shaun’s tutorial page:

            Evolution:

            $chunk = getChunk('TestChunk');
            echo $chunk;


            Not anymore. Things are much simpler . . .

            Revolution:

            $chunk = $modx->getObject('modChunk',array(
                'name' => 'TestChunk'
                ));
            
            echo $chunk->snippet;


            I’m sure the new way is more efficient, flexible, powerful, and faster, but no way it’s simpler.

            I miss getChunk. wink

            Could we maybe bring it back as a wrapper?
            I think this part of the tutorial is misleading. I’ll have to review these again...

            $modx->getChunk() is still there. And technically, all elements would produce their output by calling the process() method like so:
            <?php
            $chunk = $modx->getObject('modChunk',array(
                'name' => 'TestChunk'
            ));
            $chunkProperties = array(
                'foo' => 'This replaces a placeholder with the name TestChunk.foo',
                'bar' => 'This replaces a placeholder with the name TestChunk.bar'
            );
            echo $chunk->process();
            ?>
            or so
            <?php
            $snippet = $modx->getObject('modSnippet',array(
                'name' => 'TestSnippet'
            ));
            $snippetProperties = array (
                'param1' => 'foo',
                'param2' => 'bar'
            );
            echo $snippet->process($snippetProperties);
            ?>

            In fact, if you look at the modX class, you will see the implementation of the getChunk method for Revolution is nothing more than this, which BTW, adds the ability to set placeholders by passing an array that will become placeholders prefixed with the name of the chunk itself:
            <?php
                function getChunk($chunkName, $properties= array ()) {
                    $output= '';
                    if ($chunk= $this->getObject('modChunk', array ('name' => $chunkName), true)) {
                        $output= $chunk->process($properties);
                    }
                    return $output;
                }
            ?>
              • 3749
              • 24,544 Posts
              Quote from: OpenGeek at Oct 12, 2008, 10:49 PM

              Quote from: BobRay at Oct 05, 2008, 05:39 PM

              While messing with this, I did get some output for a while and saw the whole array of info on the current document followed by its fullly parsed content go by over and over.

              The correct answer is:

              echo $chunk->snippet

              Actually wrong; that is not the way to access the chunk output...in fact, you should never reference the snippet property except from the manager code for presenting and editing it in a form. This property will be subject to change in future versions; it remains the same in the initial release only to support legacy code with SQL queries that depend on the table/column names. This will give people time to migrate their code to the Revolution way of doing things while their old code isn’t horribly broken in the meantime.
              $modx->getChunk() is still there.

              Sorry, I did a search of the whole core for getChunk and didn’t find it. I must have misspelled it.

              I have to say that for me $chunk->process() for output is even less intuitive than $chunk->snippet, IMO. Maybe I’m just not used to it, but I’m more comfortable using the raw field content than calling a function that does stuff I may not need and don’t fully understand. I would think it would also be faster. And, AFAIK, any placeholders that have been set will be replaced later before the output is rendered.

              Also, if I want to modify a chunk’s contents (not its placeholders) programmatically, it still looks like I’d have to user $chunk->set(’snippet’, ’newValue’);

              As for migration, if the field names are going to change, it might be better to break their code now rather than later before many add-ons are converted -- or to build in an alias in the functions that use the field name. Just my $.02.

                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
                • 22303 MODX Staff
                • 10,725 Posts
                Quote from: BobRay at Oct 13, 2008, 03:49 AM

                Sorry, I did a search of the whole core for getChunk and didn’t find it. I must have misspelled it.
                It’s conveniently missing from the current API docs, so I won’t be mad. wink

                Quote from: BobRay at Oct 13, 2008, 03:49 AM

                I have to say that for me $chunk->process() for output is even less intuitive than $chunk->snippet, IMO. Maybe I’m just not used to it, but I’m more comfortable using the raw field content than calling a function that does stuff I may not need and don’t fully understand. I would think it would also be faster. And, AFAIK, any placeholders that have been set will be replaced later before the output is rendered.
                Nope, everything in Revolution is done recursively; each modElement instance is responsible for parsing it’s own cacheable content and providing the output back. You never use the source content of a modElement directly.

                Quote from: BobRay at Oct 13, 2008, 03:49 AM

                Also, if I want to modify a chunk’s contents (not its placeholders) programmatically, it still looks like I’d have to user $chunk->set(’snippet’, ’newValue’);
                Changing the source code for a modElement is a content management operation and should generally be performed through the processors in the model. Can you give me a scenario for modifying a modChunk’s fields outside of content management operations?

                Quote from: BobRay at Oct 13, 2008, 03:49 AM

                As for migration, if the field names are going to change, it might be better to break their code now rather than later before many add-ons are converted -- or to build in an alias in the functions that use the field name. Just my $.02.
                I disagree, introduce the new way on top of the old structures, get them converted to using the new API with a version for cushion, and then start changing the model to get where we want to go. At that point, the objects can have aliases for the changed field names to prevent breaking any code that does reference the old column via the new API (vs. direct SQL which would no longer work at that point at all).
                  • 3749
                  • 24,544 Posts
                  Quote from: OpenGeek at Oct 13, 2008, 10:04 AM

                  Quote from: BobRay at Oct 13, 2008, 03:49 AM

                  I have to say that for me $chunk->process() for output is even less intuitive than $chunk->snippet, IMO. Maybe I’m just not used to it, but I’m more comfortable using the raw field content than calling a function that does stuff I may not need and don’t fully understand. I would think it would also be faster. And, AFAIK, any placeholders that have been set will be replaced later before the output is rendered.
                  Nope, everything in Revolution is done recursively; each modElement instance is responsible for parsing it’s own cacheable content and providing the output back. You never use the source content of a modElement directly.

                  Not sure which statement you are saying "nope" to, but I think the placeholders definitely got replaced when I tested returning the raw content of a chunk from a snippet (though maybe I’m misremembering). I can see why you wouldn’t normally do that. If I’m right, though, it sounds like the placeholders could be being replaced twice, once by the chunk’s process() method and later, possibly by the document parser. If I can find some time, I’ll check it out.


                  Quote from: BobRay at Oct 13, 2008, 03:49 AM

                  Also, if I want to modify a chunk’s contents (not its placeholders) programmatically, it still looks like I’d have to user $chunk->set(’snippet’, ’newValue’);
                  Changing the source code for a modElement is a content management operation and should generally be performed through the processors in the model. Can you give me a scenario for modifying a modChunk’s fields outside of content management operations?

                  How about a search and replace module for chunk and resource content? Or a version control system for site content?

                  I’m currently using it because I stash some info in a chunk and modify it automatically based on the local environment the first time a snippet runs. I understand that you wouldn’t normally access the field content directly, but MODx developers are always getting their hands dirty. wink

                  Quote from: BobRay at Oct 13, 2008, 03:49 AM

                  As for migration, if the field names are going to change, it might be better to break their code now rather than later before many add-ons are converted -- or to build in an alias in the functions that use the field name. Just my $.02.


                  I disagree, introduce the new way on top of the old structures, get them converted to using the new API with a version for cushion, and then start changing the model to get where we want to go. At that point, the objects can have aliases for the changed field names to prevent breaking any code that does reference the old column via the new API (vs. direct SQL which would no longer work at that point at all).

                  Ok, but if the aliases were there now, we could make the field-name change now without breaking any code. I don’t see the advantage of waiting, but I’m probably missing something.

                  I hope these discussions are at least a little productive and I’m not just wasting your valuable time. smiley

                    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
                    • 22303 MODX Staff
                    • 10,725 Posts
                    Quote from: BobRay at Oct 13, 2008, 01:39 PM

                    Not sure which statement you are saying "nope" to, but I think the placeholders definitely got replaced when I tested returning the raw content of a chunk from a snippet (though maybe I’m misremembering). I can see why you wouldn’t normally do that. If I’m right, though, it sounds like the placeholders could be being replaced twice, once by the chunk’s process() method and later, possibly by the document parser. If I can find some time, I’ll check it out.
                    What I mean is, each element should process it’s own content before returning it’s output. Yes, they will get replaced later, by the response class (which handles noncacheable elements after everything cacheable has been handled and/or removed), but if we make the behavior consistent for all element classes, there will be many future benefits.

                    Quote from: BobRay at Oct 13, 2008, 01:39 PM

                    How about a search and replace module for chunk and resource content? Or a version control system for site content?

                    I’m currently using it because I stash some info in a chunk and modify it automatically based on the local environment the first time a snippet runs. I understand that you wouldn’t normally access the field content directly, but MODx developers are always getting their hands dirty. wink
                    Those are still content management or core operations. These can still be core extensions, but there should be functions that abstract the manipulation of the concepts of Element source content and Element output away from how they are stored in the database table. In this regard, I will be adding functions to the modElement class that are already a part of the model slated for 2.1, modElement->getContent() and modElement->setContent(). This will provide the abstraction now and we can change the implementation completely later.

                    Quote from: BobRay at Oct 13, 2008, 01:39 PM

                    Ok, but if the aliases were there now, we could make the field-name change now without breaking any code. I don’t see the advantage of waiting, but I’m probably missing something.
                    Any components people have that access these tables using SQL (e.g. via the DBAPI) would have to be changed now; I want to give folks who migrate a minor version to adjust before breaking all their custom SQL. This will give people time to learn the new OO approaches to using the API.

                    Quote from: BobRay at Oct 13, 2008, 01:39 PM

                    I hope these discussions are at least a little productive and I’m not just wasting your valuable time. smiley
                    Absolutely valuable...I wish these conversations had started months ago, but it’s never too late. smiley
                      • 3749
                      • 24,544 Posts
                      As I said on Jira, getContent() and setContent() are great ideas and were what I was looking for from the beginning. When I create an object in my own code, I try to create get() and set() methods immediately for any information that might pass to or from the object. I’m used to working in C++ where data hiding and encapsulation have been around since the beginning. It’s not clear to me yet how well they can be implemented in PHP while retaining backwards compatibility with PHP4.

                      BTW, in this case, it might make sense to call the two methods getRawContent() and setRawContent() to emphasize that they’re not dealing with the processed content and to discourage casual use of these methods.
                        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