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

    I’m attempting to output datas and parse them into templates (chunks) before displaying, with revo beta 5 api.
    Here is my buggy attempt to adapt the sample from documentation :
    			$resources = $modx->getCollection('modResource',array('published' => 1,));
    						
    			$rowChunk = $modx->getChunk('tplEntries');  
    			
    			foreach ($resources as $resource) {
    				
    				$properties = $resource->toArray();  
    				$properties['rowCls'] = 'test'; 
    				
    				$output .= $rowChunk->process($properties);
    
    
    			}
    




    In the original sample it doesn’t store any resource in a variable. It retrieves datas, and basta, like this :
    $modx->getCollection()
    


    In my case, before performing a foreach, I store the datas in $resources :
    $resources  = $modx->getCollection('modResource',array('published' => 1,)); )
    

    This causes an error when calling process() afterwards.

    When removing $resources (as in sample) it doesn’t output any error now but how do I loop through values ( because without array, foreach won’t loop) :
    $modx->getCollection('modResource',array('published' => 1,)); )
    
    foreach ($resources as $resource) {
    // stuff here will never be executed
    }
    


    Any clue would be greatly appreciated
      • 22303 MODX Staff
      • 10,725 Posts
      Here’s how I do it:
      <?php
      $output = array();
      $resources = $modx->getCollection('modResource',array('published' => 1,));
      foreach ($resources as $resource) {
          $properties = $resource->toArray();
          $properties['rowCls'] = 'test';
          $output[] = $modx->getChunk('tplEntries', $properties);
      }
      return implode("\n", $output);
      ?>

      But be careful; if you have a lot of Resources, getting a collection of all of them with all of their fields (including the content) will use a very large amount of memory. You can use xPDOQuery::select() to accomplish this (see the getResources Snippet for an example of this usage when retrieving a collection of Resources).
        • 10152
        • 156 Posts
        Thanks a lot OpenGeek