We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6531
    • 154 Posts
    I haven’t been able to find this, but I know someone out there who knows PHP could whip it up in no time.

    I’d like to have a snippet which calls a chunk and inserts a file.

    I’d call it like this:
    [tt][[Filepass?chunk=`MyChunk` &file=`/assets/media/whatever.mov`]][/tt]

    Then in [tt]MyChunk[/tt], I could use [tt][+file+][/tt] and it would get replaced with the [tt]&file[/tt].

    This would be great for me when I need to embed several flash players in a page, where all that changes is the file being referenced.

    Anyone?
      • 6531
      • 154 Posts
      WOOHOO! I can’t believe it! I did it! My first snippet!

      I call it Chunker:
      <?php
         $modx->setPlaceholder($ph, $z);
         return $modx->getChunk($tpl);
      ?>


      Call it like this:
      [tt][[Chunker?tpl=`TemplateChunk` &ph=`PlaceHolderName` &z=`TheVariable`]][/tt]

      Complex, eh?

      I can’t belive it works. MODx is amazing.
        • 6531
        • 154 Posts
        OK, now could someone help me make this so I could pass multiple placeholders?

        Something like:
        [tt][[Chunker?tpl=`wow` &ph=`var1,var2,var3` &z=`dog,cat,808`]][/tt]

        Can it be flexible to allow any number? (I can do it myself for only three!)
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Yes, in your snippet code use explode() to get the individual items from the parameter into arrays, then loop through the arrays setting the placeholder for each one. This would only work as long as your two parameters have the same number of individual items in the same order:
          $pharray = explode(',', $ph);
          $zarray = explode(',', $z);
          for($i=0;$i<count($pharray);$i++) {
             $modx->setPlaceholder($pharray[$i], $zarray[$i]);
          }
          ...
          
            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
            • 3749
            • 24,544 Posts
            I was just going to give the same answer but, as so often happens, sottwell beat me to it so here’s another solution that will only work in PHP5:

            $pharray = explode(',', $ph);  // make an array from the &ph parameter
            $zarray = explode(',', $z);   // make an array from the &z parameter
            
            
            $combined_array = array_combine($pharray,$zarray);  // make one big associative array from both
            
            foreach($combined_array as $key=>$value) {
            
                $modx->setPlaceholder($key,$value);
            
            }


            I’m fairly confident that sottwell’s solution will be faster and use less memory -- and it works in PHP4 -- so mine is just for your amusement.
              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
              • 6531
              • 154 Posts
              Fantastic. Thanks!

              (Shall I put my snippet in the Repository? wink)

              It did get me thinking that it would be nice to be able to call a Chunk with placeholders, something like
              [tt]{{SomeChunk,a=`one`,b=`2`}}[/tt]

              ...but I guess that’s why God created Snippets.
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Definitely intelligent design grin grin
                  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
                  • 7231
                  • 4,205 Posts
                  Placeholders for chunks are template/document variables, but I see what you mean. wink
                    [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                    Something is happening here, but you don&#39;t know what it is.
                    Do you, Mr. Jones? - [bob dylan]