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

    With SMARTY I can write and populate templates like this:
    Here are your products {$customer_name}:
    {foreach from=$items key=$myId item=i}
    {$i.name} x {$i.quantity} @ {$i.costperitem} total cost: {$i.totalcost}
    {/foreach}
    

    (where I pass in $items as an array with associative item attributes).

    I’d like to do the same within a chunk, but can’t figure out how to do this with the MODx placeholder syntax. I get how to replace [+customer_name+], but can’t figure out how to loop to replace an array of values. Is the MODx placeholder syntax relatively primitive (at least in Evo) - or am I missing something?

    Thanks,

    Alex
      • 4172
      • 5,888 Posts
      in modx you can for example do this:

      create a chunk:

      [+name+] x [+quantitiy+] @ [+costperitem+] total cost: [+totalcost+]


      and loop thrue your array with snippet
      example:

      <?php
      foreach ($items as $item){
      $item['totalcost']=$item['quantitiy']*$item['costperitem'];
      $output.=$modx->parseChunk($tpl,$item,'[+','+]');
      }
      return $output;
      


      then you can call it like that:
      Here are your products [!getcustomer_name!]:
      [!listproducts? &tpl=`listproductsChunk` !]




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

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 964
        • 11 Posts
        Thanks Bruno, that’s super-helpful. So essentially parseChunk() is a way of programmatically manipulating placeholders (or anything else) in a chunk at a lower level than you can with setPlaceholder() or similar.

        Thanks again,

        Alex
          • 4172
          • 5,888 Posts
          parsechunk gets the chunk and replaces the placeholders in it with your values given in your array.

          you can also have a look to jot or maxigallery how these snippets work for example.
          They use the chunkie-class to replace placeholders and parse them with phx in its chunks.
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 3749
            • 24,544 Posts
            FYI, in Revolution, I believe you could actually use Smarty for this, since it’s installed for use in the Mgr.
              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