We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9099
    • 26 Posts
    I’ve a snippet which fills an chunk in a for loop. But it seems that only last placeholder I’ve set are used in the chunk. Is there a way to get this work.
    		$output= "<ul id=\"recordlist\">";
    		for($i=0; $i<count($records);$i++){
    		
    			$cover=$records[$i][0]['value'];
    			$id=$records[$i][1]['value'];
    			$pagetitle=$records[$i][2]['value'];
    	
    			if(!isset($cover) || $cover==""){
    				$cover=$modx->config['site_url']."assets/images/cover/nocover.gif" ;
    			}
    			$modx->setPlaceholder("recordlink", $modx->makeUrl($id));
    			$modx->setPlaceholder("recordname", $pagetitle);
    			$modx->setPlaceholder("coverimage", $cover);
    			$output .= "{{recordList}}";
    		}
    		$output .= "</ul>";
    
      • 22303 MODX Staff
      • 10,725 Posts
      Since you are replacing the placeholders before the parser ever gets the chunk tags back from the snippet, that won’t work. Try this at the end of each loop...

      $output.= $modx->parseChunk('recordList', $modx->placeholders, '[+', '+]');

        • 9099
        • 26 Posts
        Thanks that it. I Thought there must be a thing like that, but I’m still at the begin learning modX.
          • 29635
          • 361 Posts
          Any reason that Ditto and Wayfinder do the replacing of placeholders "manually" rather than using this function? (Or am I missing the point completely?)

          I just did something like this 2 weeks ago but didn’t know about this function, and figured that if Ditto did it manually that must be the only way. Just curious.
            Need MODx Ecommerce? Try FoxyCart!
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: The at Feb 19, 2007, 01:04 PM

            Any reason that Ditto and Wayfinder do the replacing of placeholders "manually" rather than using this function? (Or am I missing the point completely?)

            I just did something like this 2 weeks ago but didn’t know about this function, and figured that if Ditto did it manually that must be the only way. Just curious.
            I think the authors of Ditto and Wayfinder either were unaware of this function as well, or had more complex requirements that required manually dealing with the replacements.
              • 27376
              • 576 Posts
              Quote from: OpenGeek at Feb 20, 2007, 04:15 PM

              I think the authors of Ditto and Wayfinder either were unaware of this function as well, or had more complex requirements that required manually dealing with the replacements.
              I certainly was.... I’ll document this API function on the wiki (parseChunk) asap.
                • 18397
                • 3,250 Posts
                First off, here is the function for reference:
                    function parseChunk($chunkName, $chunkArr, $prefix= "{", $suffix= "}") {
                        if (!is_array($chunkArr)) {
                            return false;
                        }
                        $chunk= $this->getChunk($chunkName);
                        foreach ($chunkArr as $key => $value) {
                            $chunk= str_replace($prefix . $key . $suffix, $value, $chunk);
                        }
                        return $chunk;
                    }
                


                There are several reasons why it is not used in Ditto:
                1. str_replace can replace an array of values with an array of values which is much faster than doing it in your own foreach loop (less function calls)
                2. If you ever have a placeholder set to the number 0 or no value (unless this bug has been fixed since I last checked) the parser will show the same result in more than one iteration
                3. A chunk name will not always be provided to Ditto as the tpl parameter can now include HTML via @CODE: or a file via @FILE:

                This is the function Ditto uses:
                    function replace( $placeholders, $tpl ) {
                		return str_replace( array_keys( $placeholders ), array_values( $placeholders ), $tpl );
                	}
                


                (I feel like I am saying the code sucks... which it doesn’t)
                  • 22303 MODX Staff
                  • 10,725 Posts
                  It’s all legacy code, so no worries if you say it sucks Mark, and it will be unnecessary in the near future anyway, as most all of these issues have been addressed in one way or another in 0.9.7

                  grin
                    • 2217
                    • 21 Posts
                    Hi,

                    can I use $modx->parsechunk using a file as template instead of a chunk?
                    I tried to use the function call
                    $modx->parsechunk("@file:" . $modx->config['base_path']. $this->template_path . $filename, $fields_values_array, '[+', '+]')
                    

                    but it returns FALSE.
                    Of course,
                    file_exists($modx->config['base_path']. $this->template_path . $filename) == TRUE
                    

                    How can I do?

                    thx in advance
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      No, what you are suggesting cannot work.

                      You’ll need to open the file and do any str_replace() or other parsing yourself.
                        Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org