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