We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13519
    • 7 Posts
    Hi, I am writing a snippet that takes an array of pages (rather than just a specific directory) and outputs them onto a single page. The issue is in the display of this data. When I have the HTML code embedded in the snippet, the data works perfectly. However, when I try to abstract the content layer out by using a chunk as the template, what ends up happening is that I get the same data displayed for as many times as there are items in the page array.

    Snippet Call on my page:
    [[ShowSetOfPages? &page_array=`34,5,6,7,8` ]]


    Snippet "ShowSetOfPages":
    $pages = isset($page_array)? $page_array : '';
    $template = isset($tpl) ? $tpl : 'SetOfPagesChunk';
    if ($pages) 
    {
    	$pages = explode(',',$pages);
    	foreach ($pages as $root)
    	{
    		$doc = $modx->getDocument($root);
    		$displayTitle = $doc['longtitle'];
    		$content = $doc['content'];
    		$modx->setPlaceholder('link', $activeRoot);		
    		$modx->setPlaceholder('displayTitle', $displayTitle);		
    		$modx->setPlaceholder('content', $content);
    		$finalString .= $modx->getChunk($template);
    	}
    }
    return $finalString;
    


    The last page (#8) gets repeated 5 times on the webpage rather than each page being displayed as expected. Here’s the example URL - http://scoba.us/test.html It’s as if the getChunk doesn’t get executed until the very end of the function. Is there any way to execute the getChunk function in a for loop or is there a workaround to get the same behavior?

    Thanks.
      • 22303 MODX Staff
      • 10,725 Posts
      Use the $modx->parseChunk() function to replace the placeholders on each loop, as described here.
        • 13519
        • 7 Posts
        Great. That worked perfectly.