We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26401
    • 247 Posts
    I’d like to convert a delimited string to an array and process each item through a chunk. is this possible out of the box, or do I need to create a snippet for this?

      • 26401
      • 247 Posts
      <?php
      // process each item in a delimited array as a chunk
      // &tpl = chunk name
      // &valname = name of the value to replace in chunk
      // &delimiter
      
      $arr = explode($delimiter,$delimString);
      foreach($arr as $val){
         $output .= $modx->getChunk($tpl,array(  
             $valname => $val
         ));  
      }
      return $output;
      
      
      


      usage:
      [[!processDelimited? &delimString=`[[*delimited-tv]]` &valname=`item` &tpl=`list`]]


      list:
      <li>[[+item]]</li>
        • 3749
        • 24,544 Posts
        getChunk() wants an associative array for that second argument. For each key in the array, It will replace the placeholders in the chunk with that keyname with the associated value:

        [[+name]]


        <?php
        $props = array('name'=>'John Doe');
        
        return $modx->getChunk('ChunkName',$props);
        ?>


        The easiest way to handle this is to put the key/value pairs in a standalone property set (let’s call it ’MyProps’ and we’ll call the snippet ’Replace’). The snippet tag and code would look something like this:

        [[!Replace? &chunkName=`myTpl` &propSet=`MyProps`]]


        <?php
        /* Replace snippet */
        $set = $modx->getObject('modPropertySet',array('name'=>$propSet));
        $props = $set->getProperties();
        $output .= $modx->getChunk($myTpl, $props);
        return $output;
        ?>


        Then, just put the placeholders in the myTpl chunk and the the placeholder names / replacement values in the MyProps property set.


          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 26401
          • 247 Posts
          howdy Bob

          the code I posted earlier does what I needed, but just wanted to check what the difference is.

          It seems that your code would do something different.

          My intent was to go from a TV that has item1,item2,item3
          to
          <ul>
            <li>item1</li>
            <li>item2</li>
            <li>item3</li>
          </ul>
            • 3749
            • 24,544 Posts
            I may have misunderstood your intent.

            Your code definitely works and doing it in your own snippet is the most efficient way. My method could do the same thing you’re doing in certain situations, but it wouldn’t work if there are a variable number of items in the comma-delimited list.

            You might want to just get the chunk with getChunk(), use str_replace to plug in your values, and append the result to $output. It would be faster.

            Much faster yet would be to skip the chunk and just do this (unless different chunks will have different formats):

            $arr = explode($delimiter,$delimString);
            $output = '<ul>';
            foreach($arr as $val){
               $output .= '<li>' . $val . '</li>';  
            }
            $output .= '</ul'>;
            return $output;


              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting