We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 31008
    • 5 Posts
    Not much to this one, but fills in some functionality that appears to be missing from MODx. If this functionality already exsists somewhere, let me know.

    Snippet name: [[SuperChunk]]
    Description: Get a chunk then find and replace given placeholders
    <?php
    /*******************************************************************\
    |     Snippet Name:  SuperChunk                                     |
    |     Created By:    Alastair L. Drong                              |
    |     Last Modified: Alastair L. Drong 02/12/07                     |
    |     Input:         &scChunkName -- name of the chunk to use       |
    |                    &sc_csv_PeqV -- user defined placeholder=value |
    |     Output:        returns chunk with values ('V')                |
    |                    substituted for placeholders ('P')             |
    \*******************************************************************/
    
         $scChunk=isset($scChunkName)? $modx->getChunk($scChunkName):'';
         $sc_strAr_PeqV=isset($sc_csv_PeqV)? explode(', ', $sc_csv_PeqV):array('error_eq_error');
    
         foreach($sc_strAr_PeqV as $scV)
          {
              $scTmpP0V1=explode('_eq_', $scV);
              $scChunk=str_replace('[+'.$scTmpP0V1[0].'+]', $scTmpP0V1[1], $scChunk);
          }
    
         return $scChunk;
    ?>


    Chunk name: scPageFormat
    here’s the chunk my example will use:
    <img src="[+chicagoIMG+]"><br />[+salutation+]


    Here’s a simple example on use:
    [[SuperChunk? &scChunkName=`scPageFormat` &sc_csv_PeqV=`salutation_eq_hello, chicagoIMG_eq_http://euclid.nmu.edu/~adrong/jpegs/chicago/IMG_1698.jpg`]] 


    output should be:
    <img src="http://euclid.nmu.edu/~adrong/jpegs/chicago/IMG_1698.jpg"><br />hello


    I haven’t tested it much yet, so I don’t promise it’ll handle all input (white space, unusual characters like é or ~) exactly as it should. Next update will support commas in output.
      • 7455
      • 2,204 Posts
      Why not use template variables for this?
      create 2 tv’s
      one with name chicagoIMG set as image and widget also as image
      and one with the name saluation set as text and no widget selected


      then the chunk would be:
      [*chicagoIMG*]"<br />[*salutation*]
      


      and no need for a snippet
      but this ofcourse will only work if you need it one
      else you could indeed use the snippet you made.
        follow me on twitter: @dimmy01
        • 31008
        • 5 Posts
        Thanks, I’ll play around with TVs (I’m still learning my way around), but I think for my case SuperChunk is the way to go as [+chicagoIMG+] and [+salutation+] will change according to the page (read: the example is the ’hello world’ version of what I’m doing).
          • 22303 MODX Staff
          • 10,725 Posts
          Nice contribution.

          On a related note, in the 0.9.7 branch (preview coming soon), Chunks already automatically turn all properties into placeholders prefixed with the chunkname just before the content is processed by the parser. This means you can just call a chunk with parameters and the placeholders in the chunk will automatically be replaced. Here is a sample Chunk tag in 0.9.7:
          [[$ChunkName? &param1=`blah` &param2=`[[SnippetForParam2Value]]`]]

          and a sample API usage...
          $modx->getChunk('ChunkName', array ('param1' => 'blah', 'param2' => $modx->runSnippet('SnippetForParam2Value'))));


          Please note, in 0.9.7, the tag format is changing slightly, so {{Chunk}} will become [[$Chunk]], [*TVorDocField*] will become [[*TVorDocField]], [+placeholder+] will become [[+placeholder]], etc. Don’t worry, migration/translation tools and guides will be available with the preview.
            • 31008
            • 5 Posts
            Ah. cool. I had actually tried that before making SuperChunk. I’ll keep it in mind when I upgrade!