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

    Sorry, I’m not good at explaining these things.

    Perhaps better if I show you:


    //Cycle through the results.
    for($i=0; $i < $rssFeedLimit; $i++) {

    //Assign the data
    $rssUrl = $xml->channel->item[$i]->link;
    $rssTitle = $xml->channel->item[$i]->title;
    $rssDesc = $xml->channel->item[$i]->description;

    // Set placeholders:
    $modx->setPlaceholder(’rssUrl’, $rssUrl);
    $modx->setPlaceholder(’rssTitle’, $rssTitle);
    $modx->setPlaceholder(’rssDesc’, $rssDesc);

    // Return the chunk
    return $modx->getChunk(’rssListResult’);

    }

    Is it possible to ’combine’ chunk data? In my rssListResult chunk I have the following:

    <a href="[+rssUrl+]">[+rssTitle+]</a>[+rssDesc+]<br />
    


    However, the above code only outputs the first result. When I attempt to use $ouput .= $modx->getChunk(’rssListResult’). It just outputs the same thing x amount of times.

    I probably have something out of place, but cannot figure it out. Can anyone shed some light on this for me?

    Thanks.
      • 22303 MODX Staff
      • 10,725 Posts
      Use $modx->parseChunk() to parse data in the local "scope" of the chunk, e.g.
      <?php
         $output = '';
         //Cycle through the results.
         for($i=0; $i < $rssFeedLimit; $i++) {
        
            //Assign the data
            $data = array(
               'rssUrl' => $xml->channel->item[$i]->link;
               'rssTitle'  => $xml->channel->item[$i]->title;
               'rssDesc' => $xml->channel->item[$i]->description;
            );
      
            // Parse the chunk with the data
            $output.= $modx->parseChunk('rssListResult', $data);
         }
        return $output;
      ?>

      Other placeholders will be replaced from outside that scope on a separate pass of the parser.
        • 10395
        • 96 Posts
        Thanks, though, I’m still not able to get the placeholders working correctly. Do you mean they need to be placed outside the loop? Right now it doesn’t display anything. I’m not sure what you mean by the placeholders need to be set separately outside the scope? I tried adding the place holders inside the loop as well as out and it just had the same result as before...

        Thanks, and sorry for needing my hand held through this. I’m not seeming to understand it sad.
          • 10487 MODX Staff
          • 1,535 Posts
          Opengeek’s code is what you need but you might need to specify the placeholder prefix and suffix, like:
          $output.= $modx->parseChunk('rssListResult', $data, '[+', '+]');
            Garry Nutting
            Senior Developer
            MODX, LLC

            Email: [email protected]
            Twitter: @garryn
            Web: modx.com
            • 4041
            • 788 Posts
            Try this snippet and see if it helps, I find sometimes you have to unset the variables in a for loop.


            $output ="";
            
            for($i=0; $i < $rssFeedLimit; $i++) {
            
               //Assign the data
               $rssUrl = $xml->channel->item[$i]->link;
               $rssTitle  = $xml->channel->item[$i]->title;
               $rssDesc = $xml->channel->item[$i]->description;
            
               // Set placeholders:
               $rssArray = array(
                            'rssUrl'    => $rssUrl,
                            'rssTitle'  => $rssTitle,
                            'rssDesc'   => $rssDesc
                            );
               // Return the chunk
              $output .= $modx->parseChunk('rssListResult', $rssArray, '[+', '+]');
              
              unset($rssUrl);
              unset($rssTitle);
              unset($rssDesc);
            
            }
              return $output;
            


            edit: sorry I had it a bit messed up but that should work now
              xforum
              http://frsbuilders.net (under construction) forum for evolution
              • 10395
              • 96 Posts
              Thanks! That worked perfect. I’m assuming the chunk placeholders are being created via the array? So, if I added another array value, it would auto create it as a placeholder?

                • 22303 MODX Staff
                • 10,725 Posts
                They don’t actually become placeholders; whatever is passed in that array is replaced in the chunk directly when the array keys appear within the placeholder tag syntax.  This prevents any local values from overwriting placeholders in the global scope with the same name.