We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22770
    • 285 Posts
    I think I’ve reached the point where my brain can’t handle modx. grin

    Here’s what I’m trying to do:

    I’m creating a fairly simple snippet that will loop through various documents and output a list. I want to allow the users to write a chunk to format the output and select what to include. In the chunk, I want them to be able to use placeholders based on either the standard document fields or any of the template variables used by the documents. So the chunk might have:

    [+pagetitle+]
    [+content+]
    [+firsttemplatevariable+]

    or whatever the user wanted.

    Now, I know how write almost all of the snippet, such as looping through the various documents, but then I hit the problem. How do I actually parse the chunk for each document and output the result? I assume that I need to use parseChunk, but I can’t figure out how to use it.

    I realise a lot of what I need to do is already done by Ditto, but I’ve looked at the Ditto snippet and it’s too complex for me.

    Any hints?
      • 7923
      • 4,213 Posts
      You loop through your records and parse the chunk for each record and concatenate them to some string variable what you send to the browser in the end.. like so:

      <?php
      //some variables for the template names.. 
      $mainchunkname = "myOuterChunkTpl";
      $rowchunkname = "myChunkTpl";
      // your sql or what ever gets your data into an array
      $sql = "SELECT * FROM ....";
      $rs=$modx->db->query($sql);
      $chunkrows="";
      // go trough the array
      while($row=$modx->fetchRow($rs)) {
         //get the chunk and parse it for this row
         $chunk = $modx->getChunk($rowchunkname);
         $chunkrows .= str_replace('[+firstname+]', $row['firstname'], $chunk);
         $chunkrows .= str_replace('[+lastname+]', $row['lastname'], $chunk);
      }
      // just another example to merge the rows in to some outer template
      $chunk = $modx->getChunk($mainchunkname);
      $output = str_replace('[+myrows+]', $chunkrows, $chunk);
      // return the output to MODx
      return $output;
      ?>
      


        "He can have a lollipop any time he wants to. That's what it means to be a programmer."
        • 22770
        • 285 Posts
        Hi doze.

        That looks really good, and very helpful. My only problem would be if people can define their own tvs and use these in the chunk, how do I know what to put in lines like this:

        $chunkrows .= str_replace('[+firstname+]', $row['firstname'], $chunk);
          • 11975
          • 2,542 Posts
          Hi,

          you can use a function like this one
          you’ll have to build an associative array first where the keys are the placeholder Names
          <?php //
          
          	function renderHtml($chunkHTML,$htmlTpl){
              global $modx;
                  if(!is_array($chunkHTML)){
                  
                  return "ca coince";
                  
                  }else{  
                  
                  $replace= array ();
                  $matches= array ();
          //find the placeholders in the chunk 
                  if (preg_match_all('~\[\+(.*?)\+\]~', $htmlTpl, $matches)) {
                      $cnt= count($matches[1]);            
                      $v = $chunkHTML ;
                      
                      for ($i= 0; $i < $cnt; $i++) {                
                           $key= $matches[1][$i];
                           $replace[$i]= $v[$key];
                      }
                      $html = str_replace($matches[0],$replace, $htmlTpl);
                     
                     return $html;      
                  }
                  return $htmlTpl;
                  }
                    
              }
          
          


          hope that helps
            Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
            • 22770
            • 285 Posts
            Awsome! That’s exactly what I’m looking for! Thanks to both of you.
              • 22770
              • 285 Posts
              Just one question: am I right that, in your code, chunkHTML is the associative array with placeholder names as keys and what as values? The row from the query that doze has in his str_replace?
                • 11975
                • 2,542 Posts
                Hi,

                here is the function used in the sample code from doze

                <?php 
                
                $htmlTpl = $modx->getChunk($rowchunkname);
                
                while($row=$modx->fetchRow($rs)) {
                   //get the chunk and parse it for this row
                   $chunkHTML ['firstname'] = $row['firstname'];
                   $chunkHTML ['lastname'] =  $row['lastname'];
                }
                
                $output = renderHtml($chunkHTML,$htmlTpl);
                
                  Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
                  • 22303 MODX Staff
                  • 10,725 Posts
                  You could also use the API function built-in for this purpose... $modx->parseChunk(chunkName, replaceVars, prefix, suffix), i.e.

                  $modx->parseChunk('myTpl', $modx->placeholders, '[+', '+]');
                    • 11975
                    • 2,542 Posts
                    Oops I forgot this one, very handy
                    thx for the reminder

                    but If you want to use file system to store your chunk you cannot use this one(parseChunk).

                    :-)
                      Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
                      • 22770
                      • 285 Posts
                      Quote from: OpenGeek at Jul 03, 2007, 09:59 AM

                      You could also use the API function built-in for this purpose... $modx->parseChunk(chunkName, replaceVars, prefix, suffix), i.e.

                      $modx->parseChunk('myTpl', $modx->placeholders, '[+', '+]');


                      How exactly would you use that? Would you simply put it into the while loop, like this:

                      while($row=$modx->fetchRow($rs)) {
                         $output .= $modx->parseChunk('myTpl', $modx->placeholders, '[+', '+]');
                      }

                      Or is it more complicated than that?