We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12241
    • 80 Posts
    SOLVED: UPDATED CODE WITH WORKING VERSION WITH NON-WORKING PARTS COMMENTED OUT FOR REFERENCE

    Okay, so I’m working on a custom snippet that is suppose to grab a resource via id and populate a chunk’s placeholders with the resources template variable values.

    I can get all of the variables I need and I can even populate them into the chunks placeholders.

    The problem is when I use multiple instances of the snippet on one page the final content is always duplicated between calls.

    I’m using unset($parameter) in there and I can confirm that the actual values are updating... it’s just the final html is always duplicated, as if it’s being cached somehow.

    Any ideas?

    Here is the snippet code:
    <?php
    //modx evo 1.0.2
    //video snippet
    /*
    [!Video? &id="[document-id]" &width=`[video-width]` &height=`[video-height]` &align=`[video-alignment]` &tpl=`[chunk]`!]
    */
    
    if (isset($tpl)){
    
     if ($tpl == 1){
          $tp = "youtube_video_template";
    
       } elseif ($tpl == 0) {
          $tp = "youtube_video";
    
       } else {
          $tp = $tpl;
       } 
    } else {
      $tp = "youtube_video";
    }
    unset($tpl);
    
    $docID = (isset($id)) ? $id : 70;
    unset($id);
    
    $tvArray = array('video_id','video_width','video_height','introtext','pagetitle','longtitle'); //array of the TV names you want to retrieve
    $tvs = $modx->getTemplateVars($tvArray,'*',$docID);
    
    //set variables retrieved from template variables
    $video_id = $tvs[0]['value'];
    $video_width = $tvs[1]['value'];
    $video_height = $tvs[2]['value'];
    $introtext = $tvs[3]['value'];
    $pagetitle = $tvs[5]['value'];
    $longtitle = $tvs[4]['value'];
    
    unset($tvs);
    
    //set placeholders
    $w = (isset($width)) ? $width : $video_width;
    $h = (isset($height)) ? $height : $video_height;
    
    //set css styles for template
    $styles = 'style="';
    if (isset($align)) {
     if ($align == 'left'){
       $styles .=  'float:left;';
      } elseif($align == 'right'){
       $styles .=  'float:right;';
     } elseif($align == 'center'){
       $styles .= 'float:none;display:block;margin:0 auto;';
     }
    } else {
     $styles .= 'float:none;display:block;';
    }
    $styles .= 'width:'.$w.'px;"';
    
    unset($align);
    
    print_r('video_id:'.$video_id.',<br> width:'.$width.',<br> height:'.$height.',<br> pagetitle:'.$pagetitle.',<br> longtitle:'.$longtitle.',<br> introtext:'.$introtext.',<br> styles:'.$styles.',<br> tp:'.$tp.'<br>');
    /* does not work for multiple instances of snippet on page
    $modx->setPlaceholder('id', $video_id);
    $modx->setPlaceholder('width', $width);
    $modx->setPlaceholder('height', $height);
    $modx->setPlaceholder('pagetitle', $pagetitle);
    $modx->setPlaceholder('longtitle', $longtitle);
    $modx->setPlaceholder('introtext', $introtext);
    $modx->setPlaceholder('styles', $styles);
    */
    $params['id'] = $video_id;
    $params['width'] = $width;
    $params['height'] = $height;
    $params['pagetitle'] = $pagetitle;
    $params['longtitle'] = $longtitle;
    $params['introtext'] = $introtext;
    $params['styles'] = $styles;
    
    //$output = $modx->getChunk($tp); //<-does not work for multiple instances of snippet on page
    $output = $modx->parseChunk($tp, $params, '[+', '+]');
    //
    unset($id, $docID, $tvs, $tpl, $tp, $video_id, $width, $height, $pagetitle, $longtitle, $introtext, $styles);
    
    //return $output;
    print_r('<textarea>'.$output.'</textarea>');//debug
    

    *UPDATED - made inner variables different from GET variables
      Environment:
      modx: rev 2.0.8-pl
      localhost: Apache/2.0.63 (Unix) PHP/5.2.11 DAV/2, MySQL client version: 5.1.37
      • 36404
      • 307 Posts
      hi,

      curious... but, the first thing i would try would be not to use the same var name for your GET var and your internal one as it may mess up your result changing the value of your $tpl on the fly inside your conditions list

      i guess you snippet will parse the $tpl chunk then i would change this for $tp or $templ for example to do if( $tpl == 1 ) $templ = "youtube_video_template" and the parse the $templ chunk

      have swing
        réfléchir avant d'agir
        • 12241
        • 80 Posts
        Hi virtualgadjo, thanks for help. I just changed over the GET variables from the internal variables (updated in code example above) as you suggested however still get the same results.

        I can’t seem to get the $output to be unique for each snippet instance. When I have more than one snippet call the $output always ends up being the duplicate output of the last snippet call on the page... it’s like I can’t unset() it between calls... even though adding unset($output); at the beginning of the script doesn’t do anything...
          Environment:
          modx: rev 2.0.8-pl
          localhost: Apache/2.0.63 (Unix) PHP/5.2.11 DAV/2, MySQL client version: 5.1.37
          • 12241
          • 80 Posts
          Okay, I solved it. The problem was using $modx->getChunk(); ... switching to $modx->parseChunk(); solved this issue.

          I’ve updated the code above with my working copy with the non-working parts commented out in case anyone else wants to learn from this smiley

          Cheers,
            Environment:
            modx: rev 2.0.8-pl
            localhost: Apache/2.0.63 (Unix) PHP/5.2.11 DAV/2, MySQL client version: 5.1.37