We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10183
    • 6 Posts
    Hey, ive been looking around for a bit and havnt found any really easy way to do this without doing things in what i think is a very ugly way so hopefully the community has some thoughts or answers on the subject.

    Whenever a snippet or chunk is called the variables present are always in reference to the current resource that is being accessed during the request, what im looking to do is create a snippet that will call and render a chunk in the context of a different resource. Example:

    Snippit Code:
    <?php
    
    $items = $modx->getCollection('modResource', array('parent' => $modx->documentIdentifier));
    
    $output = array();
    foreach ($items as $item)
    {
            $output[] = $modx->getChunk('mychunk'); // <-- something i need to do here im hoping
    }
    
    return implode("<br>", $output);
    


    ’mychunk’ Chunk Code:

    [[*id]] - [[*pagetitle]]
    
    


    I would like this code to then output a list of the ids and titles for all of the child resources of the current document. But what i get instead is just the current document over and over again. Any ideas? (Ideally without parsing the variables myself in the chunk)
      • 3749
      • 24,544 Posts
      You’re very close, actually.

      <?php
      //$items = $modx->getCollection('modResource', array('parent' => $modx->documentIdentifier));
      // That would work, but this should be a little more efficient 
      $items = $modx->resource->getMany('Children'); // note the capital C in children
      
      // You might want some version of this instead:
      // $items = $modx->resource->getMany('Children', array('published'=>'1',  'deleted'=>'0',  'hidemenu'=>'0')); 
      
      
      $output = array();
      foreach ($items as $item)
      {
              //$output[] = $modx->getChunk('mychunk'); // <-- something i need to do here im hoping
                  $output[] = $modx->getChunk('mychunk', $item->toArray()); // <--  one little change
      }
      
      return implode("<br>", $output);
        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
        • 3749
        • 24,544 Posts
        BTW, to answer your original question, if you wanted the children of *another* resource, you’d just do this in the first two lines:

        <?php
        $resource = $modx->getObject('modResource', array('pagetitle'=>'MyDocument'));
        // Or
        //$resource = $modx->getObject('modResource', $id));
        
        $items = $resource->getMany('Children');


        More info here: http://bobsguides.com/revolution-objects.html
          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
          • 10183
          • 6 Posts
          Thanks a lot!
            • 10183
            • 6 Posts
            Hrmm, actually upon closer inspection it appears that this wont quite work. What im essentially trying to do is create a blog component with a chunk that represents a single post. I then have a snippet that will call to this chunk over and over and ideally replace its contents with the item of that post for the homepage, then at the same time use that same chunk for the individual pages.

            Using what you said above i need to use [[+variablename]] in order to access the elements of the item but what i want to use is [[*variablename]] so that it will work both when the blog post is viewed directly as well as on the page that lists all posts.
              • 3749
              • 24,544 Posts
              One solution is to not use a chunk or snippet on the page that shows the post itself, just put the [[*variableName]] tags directly in the template for that page.
                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