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
    A custom snippet should do it. This is completely untested but something like it should do what you want:

    [[!getMyResources? &parents=`[[*parent]]`&tpl=`drawerTpl` &tvId=`##`]]
    


    Replace ## with the ID of the TV. And put a placeholder for it in the Tpl chunk: [[+MyTv]].

    In the snippet, be sure to use double quotes where they appear here.

    <?
    /* getMyResources snippet */

    $tv = $modx->getObject('modTemplateVar', $scriptProperties['tvId']);

    if (empty($tv)) {
    return 'Failed to get TV';
    }

    $resources = $modx->getCollection('modResource', array ('parent'=>$scriptProperties['parent']));

    $output = "<ul>\n";

    if ( ! empty($resources) ) {
    $max = count ($resources);
    $i = 1;
    foreach($resources as $resource) {
    $o = $modx->getChunk($scriptProperties['tpl'], $resource->toArray() );
    $output .= "\n " . str_replace('[[+MyTv]]', $tv->renderOutput($resource->get('id')), $o);

    if ($i%3 == 0 && $i < $max) {
    $output .= "\n</ul>\n<ul>\n";

    }
    }

    $output .= "\n</ul>\n";


    } else {
    return 'No Resources Found';
    }

    return $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
      A custom snippet should do it. This is completely untested but something like it should do what you want:

      [[!getMyResources? &parents=`[[*parent]]`&tpl=`drawerTpl` &tvId=`##`]]
      


      Replace ## with the ID of the TV. And put a placeholder for it in the Tpl chunk: [[+MyTv]].

      In the snippet, be sure to use double quotes where they appear here.

      <?
      /* getMyResources snippet */
      
      $tv = $modx->getObject('modTemplateVar', $scriptProperties['tvId']);
      
      if (empty($tv)) {
          return 'Failed to get TV';
      }
      
      $resources = $modx->getCollection('modResource', array ('parent'=>$scriptProperties['parent']));
      
      $output = "<ul>\n";
      
      if ( ! empty($resources) ) {
          $max = count ($resources);
          $i = 1;
          foreach($resources as $resource) {
              $o = $modx->getChunk($scriptProperties['tpl'], $resource->toArray() );
              $output .= "\n   " . str_replace('[[+MyTv]]', $tv->renderOutput($resource->get('id')), $o);
      
               if ($i%3 == 0 && $i < $max) {
                   $output .= "\n</ul>\n<ul>\n";
                  
               }
           }
      
          $output .= "\n</ul>\n";
      
      
      } else {
         return 'No Resources Found';
      }
      
      return $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
        • 33968
        • 863 Posts
        How about this: a 'wrapper' snippet for getResources which will take all getResources parameters and respect published status, permissions, etc.

        It requires two extra params, group (ie. group by every 3 resources) and groupTag (html tag to wrap with, such as ul).

        Call like this:
        [[groupResources? &group=`3` &groupTag=`ul` &...getResources params here... ]]
        


        Snippet code (save as groupResources):
        <?php
        $output = '';
        if(!isset($scriptProperties['outputSeparator'])) {
            $scriptProperties['outputSeparator'] = '||';
        }
        
        $rows = $modx->runSnippet('getResources', $scriptProperties);
        $rows = explode($scriptProperties['outputSeparator'],$rows);
        
        foreach ($rows as $i => $row) {
            $output .= $row;
            if (($i + 1) % $scriptProperties['group'] == 0) {
                $output .= "\n</".$scriptProperties['groupTag'].">\n<".$scriptProperties['groupTag'].">\n";
            }
        }
        
        return $output;
        


        For ihallin's example the snippet call would be:
        [[groupResources? &group=`3` &groupTag=`ul` &parents=`[[*parent]]` &tpl=`drawerTpl` &includeTVs=`1` &processTVs=`1` &tvPrefix=`` &limit=`0` ]]
        
        [ed. note: okyanet last edited this post 15 years ago.]
          • 36521
          • 9 Posts
          Thanks guys. I'll give these methods a try as soon as I get a chance. The wrapper snippet is essentially what the Evo snippet I was using was doing.
            • 13445 ☆ A M B ☆
            • 18 Posts
            This read something I really needed for a client site I am currently building. And in multiple places on their site. I took Lucas's idea and expanded on it. It worked okay but the opening and closing tags got a bit harry when output-ed.

            It's one of my first forays into actually writing my own more complex snippets as well. Here is my groupResources snippet code:
            <?php
            
            $output = '';
            
            if (!isset($scriptProperties['outputSeparator'])) {
                $scriptProperties['outputSeparator'] = '||';
            };
            
            if (!isset($scriptProperties['groupTag'])) {
                $scriptProperties['groupTag'] = 'li';
            };
            
            if (!isset($scriptProperties['wrapperTag'])) {
                $scriptProperties['wrapperTag'] = 'ul';
            };
            
            if (!isset($scriptProperties['wrapper'])) {
                $scriptProperties['wrapper'] = '1';
            };
            
            $rows = $modx->runSnippet('getResources', $scriptProperties);
            $rows = explode($scriptProperties['outputSeparator'], $rows);
            
            if ($scriptProperties['wrapper'] == 1) {
                $output .= "\n<" . $scriptProperties['wrapperTag'] . " ";
            };
            
            if (isset($scriptProperties['wrapperClass']) && $scriptProperties['wrapper'] == 1) {
                $output .= "class='" . $scriptProperties['wrapperClass'] . "'";
            };
            
            if ($scriptProperties['wrapper'] == 1) {
                $output .= ">";
            };
            
            if (isset($scriptProperties['groupClass'])) {
                $output .= "\n<" . $scriptProperties['groupTag'] . " class='" . $scriptProperties['groupClass'] . "'>\n";
            } else {
                $output .= "\n<" . $scriptProperties['groupTag'] . ">\n";
            };
            
            foreach ($rows as $i => $row) {
                $output .= $row;
                if (($i + 1) % $scriptProperties['group'] == 0 && ($i + 1) !== count($rows)) {
                   
                    if (isset($scriptProperties['groupClass'])) {
                        $output .= "\n</" . $scriptProperties['groupTag'] . ">\n<" . $scriptProperties['groupTag'] . " class='" . $scriptProperties['groupClass'] . "'>\n";
                    } else { $output .= "\n</" . $scriptProperties['groupTag'] . ">\n<" . $scriptProperties['groupTag'] . ">\n";
            		};
                }
            };
            
            if ($scriptProperties['wrapper'] == 1) {
                $output .= "\n</" . $scriptProperties['groupTag'] . ">\n</" . $scriptProperties['wrapperTag'] . ">";
            } else {
                $output .= "\n</" . $scriptProperties['groupTag'] . ">";
            };
            
            return $output;
            


            I call it the same way as well but added some extra options:
            [[groupResources? &group=`3` &...getResources params here... ]]


            It automatically wraps the output in a ul tag, and each group in it's own li tag. I've added a few extra parameters

            • &wrapperTag - defaults to ul, but lets you set it to any tag you wish
            • &groupTag - defaults to li, but lets you set it to any tag you wish
            • &wrapperClass - sets a class to the wrapper tag
            • &groupClass - sets a class to the group tag
            • &wrapper - turns on and off the wrapper, defaults to on (1) set to 0 if you want it off

            for ihallin's example you'd just need:
            [[groupResources? &group=`3` &parents=`[[*parent]]` &tpl=`drawerTpl` &includeTVs=`1` &processTVs=`1` &tvPrefix=`` &limit=`0` ]]


            The call:
            [[groupResources? &group=`3` &wrapperTag=`div` &wrapperClass=`slideshow` &groupTag=`div` &groupClass=`slide` &...getResources params here... ][

            Should generate something like:
            <div class="slideshow">
               <div class="slide">
                 ContentA
                 ContentB
                 ContentC
               </div>
               <div class="slide">
                 ContentD
                 ContentE
               </div>
            </div>
            


            I hope this helps!
              Senior Designer at Arketi Group - http://www.arketi.com
              Twitter - @jalterixnar - https://twitter.com/jalterixnar

              “There is no dishonor in failing, only in the failure to try.” – Elder fire dragon Kaernyth
              • 30319
              • 406 Posts
              jalterixnar, thank you for posting this smiley
                • 36926
                • 701 Posts
                Quote from: okyanet at Aug 26, 2011, 04:27 AM
                How about this: a 'wrapper' snippet for getResources which will take all getResources parameters and respect published status, permissions, etc.

                It requires two extra params, group (ie. group by every 3 resources) and groupTag (html tag to wrap with, such as ul).



                Snippet code (save as groupResources):
                <?php
                $output = '';
                if(!isset($scriptProperties['outputSeparator'])) {
                    $scriptProperties['outputSeparator'] = '||';
                }
                
                $rows = $modx->runSnippet('getResources', $scriptProperties);
                $rows = explode($scriptProperties['outputSeparator'],$rows);
                
                foreach ($rows as $i => $row) {
                    $output .= $row;
                    if (($i + 1) % $scriptProperties['group'] == 0) {
                        $output .= "\n</".$scriptProperties['groupTag'].">\n<".$scriptProperties['groupTag'].">\n";
                    }
                }
                
                return $output;
                


                For ihallin's example the snippet call would be:
                [[groupResources? &group=`3` &groupTag=`ul` &parents=`[[*parent]]` &tpl=`drawerTpl` &includeTVs=`1` &processTVs=`1` &tvPrefix=`` &limit=`0` ]]
                


                Hi,

                Thanks for that snippet ideas on this, especially Lucas as it's put me on the right track (i hope) on what i'm trying to achieve. I'd appreciate any advice on what i have so far and if there's a more efficient way of doing this as there could be a large number of resources and this is my first attempt at a custom snippet.

                What i'm trying to achieve is having x amount of columns and out putting every say 3rd resource into to a particular column.
                So if there 3 columns and 9 resources you'd get.

                <div class="col1">
                Resource 1
                Resource 4
                Resource 7
                </div>
                <div class="col2">
                Resource 2
                Resource 5
                Resource 8
                </div>
                <div class="col3">
                Resource 2
                Resource 5
                Resource 8
                </div>

                The snippet (splitGetResources) i have so far which is based on Lucas's seems to work ok:

                <?php
                $output = '';
                
                if(!isset($scriptProperties['outputSeparator'])) {
                    $scriptProperties['outputSeparator'] = '||';
                }
                 
                $rows = $modx->runSnippet('getResources', $scriptProperties);
                $rows = explode($scriptProperties['outputSeparator'],$rows);
                 
                
                for ($i=$startPos; $i <= count($rows); $i+=$increaseBy){
                	if ($i == count($rows)){
                		break;
                	}
                	
                $output .= $rows[$i];
                
                 }  
                
                 
                return $output;
                


                I've added the following params &increaseBy and also &startPos, i had hoped to use the &offset for getresource but that didn't see to work even in a basic getresource call.

                I'd then make 3 call for each column div

                <div class="col1">
                [[splitGetResources? &increaseBy=`3` &startPos=`0` &sortby=`menuindex` &sortdir=`ASC` &parents=`2` &tpl=`drawerTpl` &includeTVs=`1` &processTVs=`1` &tvPrefix=`` &limit=`0` ]]
                </div>
                <div class="col2">
                [[splitGetResources? &increaseBy=`3` &startPos=`1` &sortby=`menuindex` &sortdir=`ASC` &parents=`2` &tpl=`drawerTpl` &includeTVs=`1` &processTVs=`1` &tvPrefix=`` &limit=`0` ]]
                </div>
                <div class="col1">
                [[splitGetResources? &increaseBy=`3` &startPos=`2` &sortby=`menuindex` &sortdir=`ASC` &parents=`2` &tpl=`columnTpl` &includeTVs=`1` &processTVs=`1` &tvPrefix=`` &limit=`0` ]]
                </div>
                


                Now i'm sure there is a better way of doing this, I'm guessing firstly should trying and avoid having the 3 splitResources calls.

                Also couple of things wasn't sure about.
                1) Is it better to refer to a passed in param using $scriptProperties['param'] or am I ok just using $param like i've done
                2) My for loop kept doing 1 to many loops, so i added the if statement. Is there an alternative to that.

                Any other advice is much appreciated.

                Thanks

                B [ed. note: bennyb last edited this post 15 years ago.]
                  • 36926
                  • 701 Posts
                  Now i've actually had another go at this, this time avoiding the need for 3 calls.

                  Following call needs the cols param set, which is how many columns you want.
                  [[columns? &sortby=`menuindex` &cols=`3` &offset=`2` &sortdir=`ASC`  &parents=`2` &tpl=`drawerTpl` &includeTVs=`1` &processTVs=`1` &limit=`0` ]]
                  
                  


                  And the snippet code is:
                  <?php
                  $output = '';
                  $startPos = 0;
                  $increaseBy = $cols;
                  
                  if(!isset($scriptProperties['outputSeparator'])) {
                      $scriptProperties['outputSeparator'] = '||';
                  }
                   
                  $rows = $modx->runSnippet('getResources', $scriptProperties);
                  $rows = explode($scriptProperties['outputSeparator'],$rows);
                  
                  //Create a column div with the correct class for each column
                  for ($column=1; $column <= $cols; $column++) {
                  	$output .= "<div class=\"col".$column."\">";
                  	
                  	//output the resources for each column 
                  	for ($i=$startPos; $i <= count($rows); $i+=$increaseBy){
                  		$output .= $rows[$i];
                  	} 
                  $startPos++;
                  
                  $output .= "</div>";
                  }
                  
                   
                  return $output;
                  


                  Again any advice on what can be done better would be much appreciated.

                  Thanks

                  Ben

                  [ed. note: bennyb last edited this post 15 years ago.]
                    • 37693
                    • 47 Posts
                    shaunmorrison Reply #19, 14 years ago
                    Hey Lucas, thanks for setting me on the right path with this. Correct me if I'm wrong but that code doesn't create an opening piece of markup for the group and also leaves an open element after the last row?

                    I've adjusted your snippet and also included a way to add a class to the markup element (using the groupClass property) and added a default UL element to groupTag.
                    <?php
                    $output = '';
                    
                    $groupTag = $scriptProperties['groupTag'];
                    
                    if(!isset($scriptProperties['outputSeparator'])) {
                        $scriptProperties['outputSeparator'] = '||';
                    }
                    
                    if(!isset($groupTag)) {
                        $groupTag = 'ul';
                    }
                    
                    if(isset($scriptProperties['groupClass'])) {
                    	$markupOpen = $groupTag.' class="'.$scriptProperties['groupClass'].'"';
                    } else {	
                    	$markupOpen = $groupTag;
                    }
                    
                    $rows = $modx->runSnippet('getResources', $scriptProperties);
                    $rows = explode($scriptProperties['outputSeparator'],$rows);
                    
                    $numItems = count($rows);
                    foreach ($rows as $i => $row) {
                        $output .= $row;
                      	if ($i == 0){
                      		echo '<'.$markupOpen.'>';
                      	}
                        if (($i + 1) % $scriptProperties['group'] == 0) {
                            
                            if($i != $numItems-1) {
                                $output .= "\n</".$groupTag.">\n<".$markupOpen.">\n";
                            } else {
                            	$output .= "\n</".$groupTag.">\n";
                            }
                        }
                    }
                    return $output;
                      • 37033
                      • 6 Posts
                      Is it possible to utilize this snippet with getPage? If so, please help me understand how to go about it with some quick sample code.

                      Here is my current chunk that I would like to implement this with if it is possible... I can provide more context if this isn't clear enough, let me know.

                      [[getPage@resourcePaging? 
                      	&elementClass=`modSnippet` 
                      	&element=`getResources` 
                      
                      	&parents=`[[*id]]` 
                      	&limit=`32` 
                      	&sortby=`RAND()` 
                      
                      	&includeContent=`0` 
                      	&includeTVs=`1` 
                      	&showHidden=`1` 
                      	&hideContainers=`1` 
                      
                      	&tpl=`shopRowTpl` 
                      ]]
                      
                      [[!+page.nav:notempty=`<div class="paging"><div class="page-list">[[!+page.nav]]</div></div>`]]