We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • So I started getting more involved with MODX and PHP and created a snippet for one of my clients so she can post her facebook galleries on her MODX site by simply copying and pasting the link Facebook provides her when she clicks on the get link option.

    I'd like to get the templating working correctly right now I just copied and pasted some code I found on Bobs blog and got it working via trial and error.

    Now I'm no PHP expert which is why I would like to get it done the correct way so I can improve it and learn while doing so.

    <?php
    //I suppose this tells the snippet to look for the tpl to use
    $itemChunk = $modx->getChunk($tpl);
    
    //this extracts the album number from the link provided by facebook which is passed as a template variable in the snippets &album parameter
    $parts = explode('.', $album);
    
    //creates the url for the JSON data using the album number we extracted with explode
    $facebook_album = file_get_contents("http://graph.facebook.com/".$parts[3]."/photos?fields=source,name&limit=".$limit);
    
    //decodes the JSON data            
    $dataArr = json_decode($facebook_album, true);
    
    //Not sure what this is doing so would like clarification on this, from what I understand the tpl parameter of the snippet gets the tpl and applies it to the scriptProperties array not sure what ResoruceItem does
    $tpl = $modx->getOption('tpl',$scriptProperties,'ResourceItem');
    
    //for loop which sets a placeholder for the fields we will use from the provided JSON data
    foreach ($dataArr['data'] as $d) {
        
        //sets an imageURL placeholder with the value of source in the JSON data
        $modx->setPlaceholder('imageURL', $d['source']);
    
        //sets a name placeholder with the value of the name in the JSON data
        $modx->setPlaceholder('name', $d['name']);
        
        //tells modx to apply the chunk to the output
        $output = $modx->getChunk($tpl);
       
       //if I used return it would only display the last result in the array echo displays all of the items I think this needs to be changed
       echo $output;
    }
    


    Thanks for taking the time to check this out and help me better understand how snippet templating works.
      Benjamin Marte
      Interactive Media Developer
      Follow Me on Twitter | Visit my site | Learn MODX
      • 46654
      • 14 Posts
      I might be wrong on this but:

      <?php
      //this extracts the album number from the link provided by facebook which is passed as a template variable in the snippets &album parameter
      $parts = explode('.', $album);
       
      //creates the url for the JSON data using the album number we extracted with explode
      $facebook_album = file_get_contents("http://graph.facebook.com/".$parts[3]."/photos?fields=source,name&limit=".$limit);
       
      //decodes the JSON data            
      $dataArr = json_decode($facebook_album, true);
       
      //for loop which sets a placeholder for the fields we will use from the provided JSON data
      foreach ($dataArr['data'] as $d) {
           
          //tells modx to apply the chunk to the output
          $output .= $modx->getChunk($tpl, $d);
          
         //if I used return it would only display the last result in the array echo displays all of the items I think this needs to be changed
         
      }
      return $output;
      


      You dont really need to set placeholders unless you want to specifically set the names. You can just use [[+source]] and [[+name]]

      $output .= $modx->getChunk($tpl, $d);

      here, i incrementally add to ouput rather than returning individually.
      the second value is the array being passed through.



      getChunk($tpl, $array)
      applies the tpl values using the array.
      See docs here for getChunk:
      http://rtfm.modx.com/revolution/2.x/developing-in-modx/other-development-resources/class-reference/modx/modx.getchunk


      Just a note about PHP snippets:
      Anything you pass through the snippet with "&variable" (ie &tpl) automatically parses over to $tpl in the php script. (you can make whatever you want)

      so to use this script you might use...
      [[!getFacebookAlbums? &tpl=`facebookChunk.tpl` &parts=`whateverlinkgoeshere`]]

      &parts value is assigned to $parts and &tpl is assigned to $tpl in the script.

      Dont forget to update your chunks to use [[+source]] instead of [[+imageURL]] since we (i) removed the placeholders!

      If you wanted to clean up your script a little better you could even bypass the string processing and input the album number manually instead of the using the link and add it to the params list:

      [[!getFacebookAlbums? &album=`2501` &tpl=`facebookChunk.tpl`]]


      <?php
      
      //creates the url for the JSON data using the album number we extracted with explode
      $facebook_album = file_get_contents("http://graph.facebook.com/".$album."/photos?fields=source,name&limit=".$limit);
       
      ...
      
      [ed. note: eein!! last edited this post 10 years ago.]
        • 4172
        • 5,888 Posts
        you should also add some checks:

        <?php
        
        //get the tpl from scriptProperties, if not set, use tplFacebookAlbum as default - tpl
        $tpl = $modx->getOption('tpl', $scriptProperties, 'tplFacebookAlbum');
        $album = $modx->getOption('album', $scriptProperties, '');
        $outputSeparator = $modx->getOption('outputSeparator', $scriptProperties, '');
        
        //this extracts the album number from the link provided by facebook which is passed as a template variable in the snippets &album parameter
        $parts = explode('.', $album);
        
        //define output as empty array
        $output = array();
        if (!empty($tpl) && isset($parts[3])) {
            //creates the url for the JSON data using the album number we extracted with explode
            $facebook_album = file_get_contents("http://graph.facebook.com/" . $parts[3] . "/photos?fields=source,name&limit=" . $limit);
            //decodes the JSON data
            $dataArr = $modx->toJson($facebook_album);
            
            if (is_array($dataArr) && is_array($dataArr['data'])) {
                //for loop which sets a placeholder for the fields we will use from the provided JSON data        
                foreach ($dataArr['data'] as $d) {
        
                    //tells modx to apply the chunk to the output
                    $output[] = $modx->getChunk($tpl,$d);
        
                    //if I used return it would only display the last result in the array echo displays all of the items I think this needs to be changed
                }
            }
        }
        
        // convert the output-array to string seperated by $outputSeparator
        return implode($outputSeparator,$output);
        


        of course still room for improvements, for example logging errors to the error-log
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
        • Awesome, thanks for the feedback guys and taking the time to help me understand how all this works.
            Benjamin Marte
            Interactive Media Developer
            Follow Me on Twitter | Visit my site | Learn MODX