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

    I am wondering if it's possible to build an array of chunks in a snippet and then load them all?
    The analogy I would use is CodeIgniter, or Kohana (MVC frameworks) - When you render a page (in the most bog-standard way) you make your array of content, and an array of views, then you load it all...
    or you can have multiple loading of views...

    I have so far failed to figure out or find any reading on doing the same w/ MODx snippets.

    -Gabriel
      • 9102 ☆ A M B ☆
      • 318 Posts
      I'm not sure I understand what you mean by "load" but you should be able to build an array of chunks using getMany. Once you have them, you can do pretty much whatever you want with them, including returning their content in some form. You can get the content of a chunk with the getContent method.
        • 3749
        • 24,544 Posts
        We really need more information about what you're trying to do. Normally, you just put chunk tags in the content of the document or the template and modx gets them automatically when the page is rendered.

        If you need to process an individual chunk in some way in a snippet you can use $modx->getChunk() to get it and then do the processing.

        There's no easy way that I know of to get the chunks associated with a particular page because chunks are independent objects and are not tied to individual resources in any way.

        If you need to get data associated with a particular page, you can put the data in template variables, which *are* connected to specific resources.

        BTW, another option for processing chunks on a particular page is to use chunk tags with output modifiers. If there isn't an existing modifier that does what you want, you can easily write your own custom modifier.
          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
          • 36782
          • 109 Posts
          Hi Bob,

          Actually I think I just answered my own question... And it seems to have been a pretty daft one at that, as I was already doing something very similar!
          I try to not ask pointless questions, sorry.

          I will give an explanation, anyway.

          I'm using my own snippets wherever possible to aggregate content.
          I am pulling docs from containers and aggregating them in places like a sidebar, etc.

          I find that my clients are not very good at formatting content, so I do things like make preformatted image fields for them. I am trying to make my snippets slightly more general purpose, and was wanting to have:

          load chunk with opening block;
          if(image is blank) load chunk without formatted image block;
          else(image is present) load chunk WITH image block, and put image in it;
          if(introtext is not blank) load chunk with intro text and read-more button;
          else load chunk with content;
          load chunk with closing block;


          This is (sortof) the kind of thing one would do to render a webpage in a MVC style framework.

          It looks like I can load several chunks, though; I am looping through resources, arrayifying them and loading them into chunks, and it works. So I think I just have to follow that principal - build the output, then output the output.

          (please excuse my terminology, I'm [obviously] self-taught.)

            • 36782
            • 109 Posts
            @freejung :
            Yes, "load" is the wrong word, sorry 'bout the confusion.
            Thanks for the reply. I'm starting to get the feel of this a bit. It seems I can build my output as an array, including getting chunks, and resource fields, and then output the whole shebang...?

            aside:
            I remember when I finally started to understand how powerful was the 'array' in an MVC framwork (Codeigniter) and it blew my mind. I'm starting to get that feeling again with MODx smiley
              • 9102 ☆ A M B ☆
              • 318 Posts
              It sounds like you're basically doing what the getResources snippet does. Of course there's nothing wrong with writing your own snippets for that, except that you may be spending time re-inventing functionality that getResources already has. For simple if/then conditional templating such as you have above, you can use the If snippet inside a getResources template to format the output conditionally depending on whether a field has a value.

              I'm working on a way to do more complex conditional logic with getResources templates. We'll see if that goes anywhere.
                • 3749
                • 24,544 Posts
                It does sound like you're reinventing the wheel to some extent.

                Most of your logic could go in a Tpl chunk used by getResources to display the data. It's basic MVC. getResources gets the selected resources and displays them using the Tpl chunk as the View.
                  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
                  • 36782
                  • 109 Posts
                  Yeah, definitely I am, but my problem is that for this project I'm stuck w/ Cloud Linux hosting, which in essence seems to mean that getResources pushes the CPU usage beyond the users allotted limit, and the user gets throttled... not like throttled physically, but you know... CPU throttled.

                  So I've been writing these little snippets to take care of specific areas of the site. Although in this one I think maybe I'm taking it too far...

                  Upwards of 8 seconds for page loads with getResources on some hosting though! I am pretty much stuck sad

                  admittedly it's not getResources' fault here - it's an excellent bit of kit and I say that unreservedly. But almost 3 weeks trying to figure out the config conflict, or permissions setting that I have wrong, or something! I have to know when to move on smiley

                  [ed. note: unsub777 last edited this post 14 years, 10 months ago.]
                    • 36782
                    • 109 Posts
                    - oh, and a ps on doing it w/ chunks and filters -
                    I was having a terrible, terrible issue w/ page loading speed, and am trying not to use modifiers and instead put the logic right in the snippet wherever possible. It is helping. I can get dynamic pages loaded in very little more than cached speed, so that's good.
                      • 4172
                      • 5,888 Posts
                      Not sure, if this will work in a getResources-chunk, but may be this

                      [[getChunkIf? &placeholder=`introtext` &operator=`empty` &thenchunk=`introtextchunk` &elsechunk=`contentchunk`]]


                      could be faster than

                      [[!If? &subject=`[[+introtext]]` &operator=`empty` &then=`[[$introtextchunk]]` &else=`[[$contentchunk]]`]]


                      or using outputfilters this way, because of not processing chunks and placeholders before they go to If or to outputFilter-processing

                      [ed. note: Bruno17 last edited this post 14 years, 10 months ago.]
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!