We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32241
    • 1,495 Posts
    I would like to be able to access TV on snippet and plugin using MODx API, the only concern is the issue that some people encountered with the current API. Could you please help me decide which API that I need to use and how to use it?

    I tried getTemplateVars(), but I got an error. If somebody could help me with this it will be great. All I need is the ability to fetch the current document TV and get the text that was stored in that TV.

    Is sombody willing to point it out for me, and show me an example of how the code being called? The explanatiin in doc is not quite explainable to me.

    Thanks


    Sincere,
    Wendy Novianto
      Wendy Novianto
      [font=Verdana]PT DJAMOER Technology Media
      [font=Verdana]Xituz Media
      • 18397
      • 3,250 Posts
      Try this:
      $value = $modx->getTemplateVarOutput($idname=array($tvnames), $docid=$currentid, $published=1);
      
        • 32241
        • 1,495 Posts
        I haven’t tested the code yet, but I assume I just need to pass an string array with TV name on it in the first parameter, and it will return an array of the value, is that right? The return value, is it multi dimensional array or just single array containing just the value of the TV Based on the order of the tv name being inserted?

        another thing, somebody know the event in MODx? What’s the right event for me to tap in and change the default TV value into what I want, such as long title, title, content, and etc, before they are pasted to the whole website view?

        Thanks
          Wendy Novianto
          [font=Verdana]PT DJAMOER Technology Media
          [font=Verdana]Xituz Media
          • 4018
          • 1,131 Posts
          Basically, the getTemplateVarOutput function grabs the chosen template variables you want from the document you want. For instance, let’s say I have two template variables assigned to a template called ’docImage’ and ’sideContent’. Now, if I want to grab the output for these from a page with an ID of 42 then I would first assign a variable for the function call like this:

          $tvoutput = $modx->getTemplateVarOutput(array('docImage','sideContent'), 42, 1);


          Yes, the first parameter for the getTemplateVarOutput must be in the form of a single-dimension array. The parameter can be in the form of a comma delimited array or you can pass in a variable as the array:

          $tvnames = array('docImage','sideContent');
          $tvoutput = $modx->getTemplateVarOutput($tvnames, 42, 1);
          


          The second parameter is the document ID of the page you want to grab the TV’s from. This can, of course, be assigned a variable too. The last parameter simply tells the function to grab either published or unpublished documents. By default, it’s set to published (1) and is completely optional.

          Once you get the template variables you want assigned to a variable, you can then grab the variable output you want in much the same way you grab fields in a database:

          $imageout = $tvoutput['docImage'];
          $contentout = $tvoutput['sideContent'];
          


          Hope this helps! Now, in your last question...I’m not sure what you mean or what you’re trying to do.
            Jeff Whitfield

            "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
            • 32241
            • 1,495 Posts
            Bravado, thanks for the reply.

            As for the second question, I would like to achive the solution as stated in multi language topic that I just replied.

            I would like to change the value of the variables from default TV, such as page title or content, so that it will use the value from the TV.

            Hope you can help.


            Regards,
            Wendy Novianto
              Wendy Novianto
              [font=Verdana]PT DJAMOER Technology Media
              [font=Verdana]Xituz Media
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              I’ve been following this, and I think maybe, Wendy, you would do better with a plugin that catches the user’s language from his session or a cookie or whatever, then replaces the desired elements.

              You could use a combination of the language and the pagetitle, or the alias, to organize your TVs. If you’ve ever done an "echo" in a plugin set to the OnLoadWebDocument event, all it has at this point is the template itself, before it has begun to parse any snippets/chunks whatever. You can have your plugin parse the template and replace any TVs (or even snippets and chunks), for example [*Paragraph1-en*] would get replaced with [*Paragraph1-fr*], if it exists (the plugin would have to check to make sure it exists before making the swap). Then when it’s all finished, the parser would continue parsing using the new language TVs. These TVs could either contain actual content, or bindings to documents, chunks, or even to the database for the desired content.

              So basically, the plugin would be acting as a pre-parser.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 4018
                • 1,131 Posts
                Quote from: wendy at Dec 10, 2005, 01:01 PM

                I would like to change the value of the variables from default TV, such as page title or content, so that it will use the value from the TV.

                Ok, based on the other topic you posted to (the multi-language one), I’m assuming that you want to define a few TV’s that will act as secondary page titles and content and whatnot. What you could probably do in this situation is go ahead and define clear-cut TV’s for this and assign them to the templates you want. From there, you can create a simple snippet that simply takes the default content you wish to retrieve and returns a conditional value based something like language selection. For instance, let’s say the snippet is called ’langsetting’, a call could be made like this:

                [[langsetting?tvname=content]]

                The snippet could then be setup to output the default content or alternative content like so:

                if(isset($tvname){
                    switch ($tvname) {
                    case content:
                       if($lang = 'french'){
                           $output = "[*frcontent*]";
                       }else{
                           $output = "[*content*]";
                       }       
                       break;
                    case pagetitle:
                       if($lang = 'french'){
                           $output = "[*frpagetitle*]";
                       }else{
                           $output = "[*pagetitle*]";
                       }       
                       break;
                    }
                }
                return $output
                


                As you can see, I just used a case statement to determine what variable I want to output and each statement just determines what language is set for the $lang variable and outputs the correct template variable, else it just outputs the default content variable (pagetitle,content, etc.). Probably would need to work out the logic on how the $lang variable would work, but it’s a pretty decent solution nonetheless because it means you can have different content, pagetitles, etc. for each page. And, there’s certainly nothing wrong with having multiple calls for the snippet on each page since the case statement is pretty efficient. This is just a start...and I hope it sends you in the right direction. smiley

                Jeff
                  Jeff Whitfield

                  "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
                  • 32241
                  • 1,495 Posts
                  That’s another approach that I did right now, but I’m looking for away to use plugin and inject it right into the core system. By using this kind of approach, I’m not sacrificing all other snippets that was done already. For example, because it will act just like ordinary, and that’s another reason why MODx allow the use of plugin I believe.

                  But your idea is also great, I will release the snippet first then with that idea.
                  If you know how to tap into the default tv vars, before they were parsed, please let me know.

                  Thanks
                    Wendy Novianto
                    [font=Verdana]PT DJAMOER Technology Media
                    [font=Verdana]Xituz Media