We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3109 ☆ A M B ☆
    • 894 Posts
    So we have an xml file that contains a list of vehicles, these range from motorcycles to water crafts and their condition ie: new or used.

    We already have the XML file parsed and displaying all the entries correctly now we are trying to use a tpl for our snippet to style our listings.

    The chunk loops the right amount of times but no values are returned.

    Any help would be appreciated.

    Thanks.

    This is a sample of our array:

    
    Array(
      [Motorcycles] => Array(
        [used] => Array(
          [1] => Array(
            [listing-id] => 2228063
            [category] => Motorcycles
          )
        )
      )
    )
    
    


    This is our snippet code:

    <?php
    //Snippet:
    $i=0;
    foreach($xml->listings->listing as $listing) {
      $i++;    
      $c = (string)$listing->category;
      $n = (string)$listing->condition;
      $array[$c][$n][$i]['listing-id'] 			= (string)$listing->{'listing-id'};
      $array[$c][$n][$i]['category'] 			= (string)$listing->category;
    }
    
    $list = $modx->getChunk('listing', $array);
    $modx->setPlaceholder('listing', $list);
    
    ?>
    


    This is our chunk:

    
    
    <ul>
      <li>[[+Motorcycles][+used][+1][+listing-id]]</li>
    </ul>
    
    


    And this is our placeholder :

     [[+listing]]
    [ed. note: benmarte last edited this post 15 years, 1 month ago.]
      Benjamin Marte
      Interactive Media Developer
      Follow Me on Twitter | Visit my site | Learn MODX
      • 34012
      • 88 Posts
      First off, you are calling the chunk too late in my opinion.

      If you check some complex ready made extra like getResources, basicly they work like this (from one of my snippets)
      $products = $modx->getCollection('vcProduct', $query);
      foreach($products as $product) {
          $vars = $product->toArray();
          $out[] = $modx->getChunk($rowTpl, $vars); 
      }
      return implode("\n", $out);


      Each rown gets called separately, and then I return the output to template with that. Got ul in the template and that chunk is list items pretty much.

      instead of returning it, you can set it as placeholder to template with
      $modx->toPlaceHolder('placeHolderName' , implode("\n", $out);
      in my case

      After this, you call the snippet first in the template with [[!yourSnippet]] (you can call it also cached without using the ! in the call). Then you can put your placeholder [[+placeHolderName]] there.
      code example
      [[!YourSnippet]]
      [[+placeHolderName]]
      


      I am not sure how multidimensional arrays work for place holders, if I recall right you might need to call $modx->toPlaceHolders() http://rtfm.modx.com/display/revolution20/modX.toPlaceholders.

      Sorry, bit lagged from odd day behind. My php seems to run on bit slow
      [ed. note: lazylegs last edited this post 15 years, 1 month ago.]
        Almost retired from web-development industry but still randomly writing at Lazylegs.info and on schedule hopefully in near future to finish Oracle and PostgreSQL ports of MODX
        • 11055 ☆ A M B ☆
        • 3,112 Posts
        First thing first, getChunk only works for 1 level deep array.
        By looking your array
        Array(
          [Motorcycles] => Array(
            [used] => Array(
              [1] => Array(
                [listing-id] => 2228063
                [category] => Motorcycles
              )
            )
          )
        )
        

        That means you should have some getChunk()-s in loops (where the depth depends on your need) and 1 in a wrapper.
        1. To loop the listing-id (why do you need 'category' again if the parent key IS the category?)
        2. To loop the condition
        3. To loop the category
        4. The last one, is the wrapper template to handle all 3 results above, and it's up to you to return it directly or store it inside the placeholder.

        Without chunk
        <?php
        
        $output = '<ul>';
        foreach ($categories as $cat => $conditions) {
            $output .= '<li>' . $cat . ' ';
            foreach ($conditions as $cond => $array) {
                $output .= $cond . ' ';
                foreach ($array as $arKey => $arVal) {
                    $output .= $arKey['listing-id'];
                }
            }
            $output .= '</li>';
        }
        $output .= '</ul>';
        return $modx->setPlaceholder('listing', $output);
        

        With chunk (based on your sample output):
        $wrapperPhs = array();
        foreach ($categories as $cat => $conditions) {
            $rowPhs[$cat] = $cat;
            foreach ($conditions as $cond => $array) {
                $rowPhs[$cond] = $cond;
                foreach ($array as $arKey => $arVal) {
                    $rowPhs['listing-id'] = $arKey['listing-id'];
                    $wrapperPhs['wrapper'] .= $modx->getChunk('listingAllRowsTpl', $rowPhs) . "\n";
                }
            }
        }
        $output = $modx->getChunk('wrapperTpl', $wrapperPhs);
        
        // direct return
        return $output;
        
        // or store inside a placeholder
        return $modx->setPlaceholder('listing', $output);
        

        <!-- wrapperTpl chunk -->
        <ul>
            [[+wrapper]]
        </ul>
        

        <!-- listingAllRowsTpl chunk -->
            <li>[[+Motorcycles]] - [[+used]] - [[+1]] - [[+listing-id]]</li>
        

        Untested, but that's the idea. [ed. note: goldsky last edited this post 15 years, 1 month ago.]
          Rico
          Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
          MODx is great, but knowing how to use it well makes it perfect!

          www.virtudraft.com

          Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

          Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

          Maintainter/contributor of Babel

          Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
          • 3109 ☆ A M B ☆
          • 894 Posts
          @goldsky thanks for clarifying getChunk does not work for more than 1 level deep.

          We are using the first method you posted and it's working.

          Thanks guys for helping me out with this smiley
            Benjamin Marte
            Interactive Media Developer
            Follow Me on Twitter | Visit my site | Learn MODX
            • 11055 ☆ A M B ☆
            • 3,112 Posts
            yw, ben smiley
              Rico
              Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
              MODx is great, but knowing how to use it well makes it perfect!

              www.virtudraft.com

              Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

              Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

              Maintainter/contributor of Babel

              Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.