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

    I could do with some advice on templating a custom snippet. The snippet below outputs rows from a custom database table. It uses a rowTpl and rowClass set as parameters in the snippet call.

    How would I go about adding a 'outerTpl'. I need to be able to output the rows using a placeholder in a 'outerTpl' chunk?

    I can get the outerTpl chunk to display, but can't get the foreach loop output to replace the placeholder.

    Snippet Code:
    // add package so xpdo can be used:
    $path = MODX_CORE_PATH . 'components/its/';
    $result = $modx->addPackage('its',$path . 'model/','modx_');
    
    //$isbn = parameter value passed from snippet call
    //$outerTpl = parameter value passed from snippet call
    //$rowTpl = parameter value passed from snippet call
    //$rowClass = parameter value passed from snippet call
    //$altClass = parameter value passed from snippet call
    
    $rows = $modx->getCollection('ITS',array('isbn'=>$isbn));
    $outerTpl = $modx->getOption('tpl',$scriptProperties,'itsTpl');
    $rowTpl = $modx->getOption('rowTpl',$scriptProperties,'itsRowTpl');
    $rowClass = $modx->getOption('rowClass',$scriptProperties,'white');
    $altClass = $modx->getOption('altClass',$scriptProperties,'brown');
    
    $i = 0; //Starts row count at 0
    
    $output = $modx->getChunk($outerTpl,$resourceArray);
    
    foreach($rows as $row) {
       if($i % 2 == 0){
         $itsClass = $rowClass;
       }else{
         $itsClass = $altClass;
       }
       $resourceArray = $row->toArray();
       $resourceArray['itsClass'] = $itsClass;
       $rowOutput .= $modx->getChunk($rowTpl,$resourceArray);
       $i++;
    }
    
    return $output;


    Any help or advice would be great!

    Many Thanks

    This question has been answered by Wanze. See the first response.

      Find me on Twitter, GitHub or Google+
    • discuss.answer
      • 31946
      • 116 Posts
      Hi romanum,
      Before returning the output, pass your processed rows to the outerTpl chunk (as a placeholder) and return the outerTpl.

      $i = 0; //Starts row count at 0
       
      foreach($rows as $row) {
         if($i % 2 == 0){
           $itsClass = $rowClass;
         }else{
           $itsClass = $altClass;
         }
         $resourceArray = $row->toArray();
         $resourceArray['itsClass'] = $itsClass;
         $rowOutput .= $modx->getChunk($rowTpl,$resourceArray);
         $i++;
      }
      
      $rowsPlaceholder = array('rows' => $rowOutput);
      $output = $modx->getChunk($outerTpl,$rowsPlaceholder); 
      return $output;
      

      In your outerTpl, you should now have the Placeholder
      [[+rows]]
      to Output your Rows.