We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27541
    • 29 Posts

    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






      • 22303 MODX Staff
      • 10,725 Posts
      $tvArray[$tv_string][1] should be $tvArray[$tv_string][’value’] I believe...
        • 27541
        • 29 Posts
        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
          • 22303 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.
            • 27541
            • 29 Posts
            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;
            }
              • 27541
              • 29 Posts
              thinking i must have made some error in my snippet, i tried replacing it with the one presented on this page in the wiki:
              http://wiki.modxcms.com/index.php/Adding_TV_Widgets_without_hacking_the_core

              still nothing! could there be some setting in the manager that needs to be changed to make these work?

              thanks
              st
                • 22303 MODX Staff
                • 10,725 Posts
                Quote from: skt at Aug 18, 2008, 12:26 PM

                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;
                }
                

                I don’t see where you are doing anything with $text ? You need to return $text if you want it to be presented on the page where this snippet is called. e.g.
                <?php
                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';
                		return $text;
                	}
                } else {
                	echo 'tv_string was empty';
                	return;
                }
                ?>

                  • 27541
                  • 29 Posts
                  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;

                  ?>



                    • 27541
                    • 29 Posts
                    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
                      • 11206
                      • 73 Posts
                      i’ve used getTemplateVarOutput before, no problem.

                      here is how I have used it
                      $main_tv = $modx->getTemplateVarOutput('*',$main_id,"1"); 


                      as an example.

                      you are passing $tv_string to your getTemplateVarOutput - what is this?