We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7231
    • 4,205 Posts
    I don’t think that you have the correct syntax for $modx->getTemplateVarOutput
    $fileName = $modx->getTemplateVarOutput(array($key));
    This is missing the document ID.

    Maybe try adding the document ID like this:
    $fileName = $modx->getTemplateVarOutput($key )
    Here $key needs to have the TV’s name. I would add the $id bit outside the foreach loop since it only needs to be executed once.

    This is all I could see that could give a problem, hope this is valid grin

    http://wiki.modxcms.com/index.php/API:getTemplateVarOutput
    Edit: never mind the bit about the ID since it defaults to the current, my mistake. It should work anyway.
      [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

      Something is happening here, but you don't know what it is.
      Do you, Mr. Jones? - [bob dylan]
      • 27708 MODX Staff
      • 2,502 Posts
      Quote from: ganeshXL at Feb 13, 2008, 09:57 PM

      Call me stubborn, but I can’t see why one single TV + one snippet wouldn’t achieve the same thing as 14 TVs + a snippet.
      i’ll test this out but I still need the output to have a wrapper.

      This whole thing was based around Adding TV WIdgets Without Hacking the Core in the Wiki.

      Quote from: dev_cw at Feb 14, 2008, 07:13 AM

      I don’t think that you have the correct syntax for $modx->getTemplateVarOutput
      $fileName = $modx->getTemplateVarOutput(array($key));


      dev_cw, I have no problem extracting the data from the loop. My problems arose when I placed the loop in the function. I can still get the output but I can’t assign a variable to the function call and get a return value. If I don’t care how the output is formatted I can just place the loop in the echo statement and be done.

      TV values are tied to the document so I don’t need the id so long as I am calling it from that document. If I wanted to get the value of the TVs from another page or call it in another app I would surely need the id.

      I am going to try to pull the loop out of a function and run it as in the second.

      GaneshXL: You may be right that it is easier to use a RTE field and have the client place a list but I then have to make them manually create lists in TinyMCE and despite what most people think is not foolproof. One missing UL or LI and we could have a broken page.

      Thanks for all your help and I will hopefully have this put to bed within the hour.


      @BobRay : This is all in the manager. What I am trying to do have the client upload a file in each of the file TVs (in the Document Edit pane of the Manager and then assign a name to it. The script creates an associative array out of the TVs, then gets their values, then outputs the values one at a time into a list via a loop.

      Cheers,

      Jay
        Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
        • 14050
        • 788 Posts
        There are a couple problems with the following code from what I see:

            // Loop Through the file array and output all the information
            foreach ($file as $key => $value){
                
                $fileName = $modx->getTemplateVarOutput(array($key));
                $fileLoc = $modx->getTemplateVarOutput(array($value));
                $outFileName = $fileName[$key];
                $outFileLoc = $fileLoc[$value];
                if (empty($outFileName) || empty($outFileLoc)) {
                    return '';
                }
                $outPutFile = "<li><a href=\"".$outFileLoc."\" title=\"Click to download ".$outFileName."\">".$outFileName."</a></li>\n";
                $output = $outPutFile;
            }
            // Return $outPutFile
            return $output;
            
        }
        


        When you check to see if the TV is empty with this code:

                if (empty($outFileName) || empty($outFileLoc)) {
                    return '';
                }
        


        You should change that to:

                if (empty($outFileName) || empty($outFileLoc)) {
                    break;
                }
        


        Otherwise, if any of the TVs are empty, the function is going to return an empty string. I don’t think this is the behavior you want. Instead, it will stop going through the loop upon the first instance of an empty file TV, and then return the ouput it was building with the return statement at the end of this function. Lastly, I think you are missing a concatenation operator here:

        $outPutFile = "<li><a href=\"".$outFileLoc."\" title=\"Click to download ".$outFileName."\">".$outFileName."</a></li>\n";
        $output = $outPutFile;
        


        I believe you want:

        $outPutFile = "<li><a href=\"".$outFileLoc."\" title=\"Click to download ".$outFileName."\">".$outFileName."</a></li>\n";
        $output .= $outPutFile;
        


        Without the string concatenation, $output will just equal the last non-empty $outPutFile, instead of a string containing a concatenation of all the output files.
          Jesse R.
          Consider trying something new and extraordinary.
          Illinois Wine

          Have you considered donating to MODx lately?
          Donate now. Every contribution helps.
          • 27708 MODX Staff
          • 2,502 Posts
          I got it working with a little help from everyone! Here is the code that now works:

          <?php
          /*
          #
          #   RelatedInfo Snippet
          #   by Jay Gilmore (SmashingRed Web & Marketing)
          #
          */
          # Script Functions
          function getFiles(){
          
          # Assign a $modx as a global variable
              global $modx;
              
              $file["file1Text"]="file1";
              $file["file2Text"]="file2";
              $file["file3Text"]="file3";
              $file["file4Text"]="file4";
              $file["file5Text"]="file5";
              $file["file6Text"]="file6";
              $file["file7Text"]="file7";
              
              $output ='<h6>Related Files</h6><ul>';
              foreach ($file as $key => $value){
                  
                  $fileName = $modx->getTemplateVarOutput(array($key));
                  $fileLoc = $modx->getTemplateVarOutput(array($value));
                  $outFileName = $fileName[$key];
                  $outFileLoc = $fileLoc[$value];
                  if (empty($outFileName) || empty($outFileLoc)) {
                      break;
                  }
                  $outPutFile = '<li><a href="' . $outFileLoc . '" title="Click to download ' .$outFileName. '">' .$outFileName. '</a></li>'."\n";
                  $output .= $outPutFile;
              }
              $output .='</ul>';
                  if (!empty($outPutFile)){ 
                  return $output;
                  }
                  else{
                      print '';
                  }
              }
              
          function getLinks(){
              // Assign Variable to TV "relatedLinks"
              $links = 'relatedLinks';
               
              //Assign global scope to $modx
              global $modx;
          
              // Get Value of the RelatedLinks Template Variable
              $relatedLink = $modx->getTemplateVarOutput(array($links));
              $outLink = $relatedLink[$links];
              if (empty($outLink)){
                  return '';
              }
              if (!empty($outLink)){
              $output = '<h6>Related Links</h6>' . $outLink;
              return $output;
              }
              else {
                print '';
              }
          }
          
          # Assign function output to variables
          $getFiles = getFiles();
          $getLinks = getLinks();
          
          # Output the Output
          if (!empty($getLinks) && !empty($getFiles)){
          echo '<div class="sidepanel">';
          echo "\n<h5>Related Info</h5>";
          print $getLinks;    
          print $getFiles;
          echo '</div>';
          }
          else{
              echo '';
          }
          ?>
          


          Thanks again for all the help!

          Cheers,

          Jay
            Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
            • 14050
            • 788 Posts
            Glad to hear you got it working smiley
              Jesse R.
              Consider trying something new and extraordinary.
              Illinois Wine

              Have you considered donating to MODx lately?
              Donate now. Every contribution helps.