hello,
i’m trying to write a snippet that will take an input tv and convert it to an unordered list of links. a user would follow a specific format to specify the files to link to and the text for each link.
the current problem is that i can’t seem to get the template variable output successfully. i’m new to all of this (modx, php, mysql), so it’s likely it’s something obvious. any help is greatly appreciated.
thanks,
st
..................... snippet ListOLinks (echo statements are just for debugging & would eventually be removed)
$tv_string = isset($tv_string) ? $tv_string : false;
echo ’$tv_string = ’ . $tv_string;
$linkUrl= isset($linkUrl) ? $linkUrl : ’’;
$delim1 = ’\n’;
$delim2 = ’|’;
$docid = isset($docid) ? $docid : $modx->documentObject[’id’];
if($tv_string){
$tvArray = $modx->getTemplateVarOutput(array($tv_string));
if(!$tvArray[$tv_string][1]){
echo ’could not retrieve template variable output’;
return;
} else {
$text = $tvArray[$tv_string][1];
echo ’tv_string retrieved successfully’;
}
} else {
echo ’tv_string was empty’;
return;
}
$arList = explode($delim1, $text);
$ulOut = ’<ul>’;
foreach($arList as $listItem){
$liLine = explode($delim2, $listItem, 2);
$liText = $liLine[0];
$liFile = $liLine[1];
$ulOut .= ’<li><a href="’ . $linkUrl . trim($liFile) . ’">’ . ltrim($liText) . ’</a></li>’;
}
$ulOut .= ’</ul>’;
return $ulOut;
.................... template variable
name: showTracks.Wedding
type: Rich Text
value:
Pachelbel’s Canon | canon.mp3
Flower Duet | flower_duet.mp3
Choral Nocturne | choral_nocturne.mp3
Love One Another | love_one_another.mp3
Oh God You Search Me | oh_god_search.mp3
note: i see that the carriage returns in the tv input box are not being registered. i’ll probably have to use another delimiter... but i don’t think this is the current problem.
......................... snippet call
[!ListOLinks? &tv_string=`showTracks.Wedding` !]
.......................... resulting output
$tv_string = showTracks.Wedding
could not retrieve template variable output
-
MODX Staff
- 10,725 Posts
$tvArray[$tv_string][1] should be $tvArray[$tv_string][’value’] I believe...
thank you for the quick response!
i replaced the [1] with [’value’], but i’m still getting the same error. not sure i understand -- i had thought that getTemplateVarOutput returns an array, in which the first value is the output of the template variable?
thanks,
st
-
MODX Staff
- 10,725 Posts
Actually, it should just be $tvArray[$tv_string] -- it returns the output directly, not in an array. The array returned is that of all the TV’s that were requested, and each element contains the output of the processed TV.
thank you -- tried that, but no change. for reference, i now have:
if($tv_string){
$tvArray = $modx->getTemplateVarOutput(array($tv_string));
if(!$tvArray[$tv_string]){
echo ’could not retrieve template variable output’;
return;
} else {
$text = $tvArray[$tv_string];
echo ’tv_string retrieved successfully’;
}
} else {
echo ’tv_string was empty’;
return;
}
sorry, i had only pasted in the part of the code that i’d changed since starting the thread. here is the full version -- $text is converted to an array and the output should be a ul.
however, it does seem to me the problem is with the getTemplateVarsOutput part... (1) i had tested the second part independently & it was working fine, (2) the resulting output is still ’could not retrieve template variable output’, which means that $tv_string is not empty, but $tvArray[$tv_string] is empty, and (3) the other snippet i’d tried, which also contained getTemplateVarsOutput, did not work either.
thanks again for your help -- i’d really love to get this working.
<?php
$tv_string = isset($tv_string) ? $tv_string : false;
$linkUrl= isset($linkUrl) ? $linkUrl : ’’;
$delim1 = ’\n’;
$delim2 = ’|’;
$docid = isset($docid) ? $docid : $modx->documentObject[’id’];
if($tv_string){
$tvArray = $modx->getTemplateVarOutput($tv_string);
if(!$tvArray[$tv_string]){
echo "could not retrieve template variable output
";
return;
} else {
$text = $tvArray[$tv_string];
echo "tv_string retrieved successfully
";
}
} else {
echo "tv_string was empty
";
return;
}
$arList = explode($delim1, $text);
$ulOut = ’<ul>’;
foreach($arList as $listItem){
$liLine = explode($delim2, $listItem, 2);
$liText = $liLine[0];
$liFile = $liLine[1];
$ulOut .= ’<li><a href="’ . $linkUrl . trim($liFile) . ’">’ . ltrim($liText) . ’</a></li>’;
}
$ulOut .= ’</ul>’;
return $ulOut;
?>
hi,
just checking in to see if anyone else has tried to use getTemplateVarOutput in a snippet, and might have any ideas as to what i’ve missed.
many thanks,
skt