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

    I’ve been trying to follow the examples given here in creating snippets using chunks and placeholders:

    http://wiki.modxcms.com/index.php/Creating_Snippets

    My SQL query outputs a lot of rows, and if I echo them, I get the desired result of displaying all of them in the page. Now if I follow the custom placeholder and chunk assignment mentioned, it only gives me a single output via the chunk and placeholder. Can anyone tell me or point me to the right direction as to how to output my SQL query as if I’m echoing them within the while loop, but using placeholders and chunks?
      • 4172
      • 5,888 Posts
      use the api-function $modx->parseChunk http://wiki.modxcms.com/index.php/API:parseChunk in your loop and collect your output into an output-var like so:

      <?php
      
      //your code.............
      
      foreach($rows as $row){
      
      $output.= $modx->parseChunk($tpl, $row, '[+', '+]');
      
      }
      
      return $output;

        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 7327
        • 195 Posts
        Ah ok, seems to work, thanks for that! But what if I want to output it to a set of pre-named placeholders?
          • 3749
          • 24,544 Posts
          The problem is that you’ll be overwriting those placeholder values each time through the loop, so you’ll only get the values they have on the last iteration. That’s why you need to either use parseChunk(), getChunk() or use str_replace() each time through the loop to add the results to the $output variable.
            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