We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19889
    • 616 Posts
    Hello,
    How can I pass a tv value that is inherited to a snippet - the following doesn’t work:

    [!snippetName? & tvValue=[*tvVar*]!] and in the snippet I’ve something like:

    if(isset($_GET[’tvValue’])) {$var=$_GET[’tvValue’];}

    what am I doing wrong?

    if I try to get the value in the snippet like:

    $tv = $modx->getTemplateVar(’tvVar’, "", $modx->documentIdentifier);
    $tv = $tv[’value’]

    I just get "@inherit"??

    Thank you in advance for your help
      • 22303 MODX Staff
      • 10,725 Posts
      Try $modx->getTemplateVarOutput() instead...
        • 19889
        • 616 Posts
        am I understanding you right that I’m not able to pass the tvValue to the snippet?

        I tried this:

        $lang = $modx->getTemplateVarOutput(’lang’, "", $modx->documentIdentifier);
        $lang = $lang[’value’];

        It’s probably wrong, as I’m not getting anything?
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: mmjaeger at Jul 08, 2007, 12:29 PM

          am I understanding you right that I’m not able to pass the tvValue to the snippet?
          It depends on too many factors, but in general, no, you cannot rely on that value being parsed before the snippet is, which would need to happen in order for the proper value to be passed.

          Quote from: mmjaeger at Jul 08, 2007, 12:29 PM

          I tried this:

          $lang = $modx->getTemplateVarOutput(’lang’, "", $modx->documentIdentifier);
          $lang = $lang[’value’];

          It’s probably wrong, as I’m not getting anything?

          See http://wiki.modxcms.com/index.php/API:getTemplateVarOutput and notice the description of the first parameter...
          $idname = either an array of TV names, or * for all TVs for this document
            • 33372
            • 1,611 Posts
            Quote from: mmjaeger at Jul 08, 2007, 12:29 PM

            I tried this:
            $lang = $modx->getTemplateVarOutput(’lang’, "", $modx->documentIdentifier);
            $lang = $lang[’value’];
            I think you want to use this instead:
            $lang = $lang[’lang’];

            getTemplateVarOutput() returns an array of the values of whichever TVs you requested (or all of them), indexed by TV name.

            I think that what Jason is getting at is that the TV output won’t be parsed by this function (it will just return the value in the database regardless of output widgets and whatnot, so the function name is rather misleading). That may not matter to you in this circumstance, but if it does you’ll need to use a different method. One thing that you could try if you need a parsed TV value is passing it as a snippet parameter (e.g., [[mysnippet? &tvValue=`[*lang*]`]] ).
              "Things are not what they appear to be; nor are they otherwise." - Buddha

              "Well, gee, Buddha - that wasn't very helpful..." - ZAP

              Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
              • 19889
              • 616 Posts
              Quote from: ZAP at Jul 08, 2007, 03:15 PM

              Quote from: mmjaeger at Jul 08, 2007, 12:29 PM

              I tried this:
              $lang = $modx->getTemplateVarOutput(’lang’, "", $modx->documentIdentifier);
              $lang = $lang[’value’];
              I think you want to use this instead:
              $lang = $lang[’lang’];

              getTemplateVarOutput() returns an array of the values of whichever TVs you requested (or all of them), indexed by TV name.

              I think that what Jason is getting at is that the TV output won’t be parsed by this function (it will just return the value in the database regardless of output widgets and whatnot, so the function name is rather misleading). That may not matter to you in this circumstance, but if it does you’ll need to use a different method. One thing that you could try if you need a parsed TV value is passing it as a snippet parameter (e.g., [[mysnippet? &tvValue=`[*lang*]`]] ).

              o.k., this works:

              $ids = array(’lang’);
              $tvs = $modx->getTemplateVarOutput($ids, $modx->documentIdentifier);
              $lang = $tvs[’lang’];

              passing the TV value as a snippet parameter doesn’t work - this is what I have in the document:
              [[test?&lang=`[*lang*]`]] and this is what I have in the snippet: $lang = isset($_GET[’lang’]) ? $_GET[’lang’] : ’not found’; if I just put [*lang*] into the document, I get the correct value. maybe the snippet is executed before the value gets parsed in the document - I really don’t know.

              thanks again for your help.
                • 33372
                • 1,611 Posts
                Okeedokee. If you don’t need the parsed TV output then that’s the best way that I know of to get at it. Sometimes I’ve been able to pass TVs as snippet parameters, and sometimes I’ve had trouble (it all depends upon the cycles of parsing the document content, which is not always easy to determine or wise to rely on).
                  "Things are not what they appear to be; nor are they otherwise." - Buddha

                  "Well, gee, Buddha - that wasn't very helpful..." - ZAP

                  Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
                  • 22303 MODX Staff
                  • 10,725 Posts
                  Quote from: ZAP at Jul 08, 2007, 03:15 PM

                  I think that what Jason is getting at is that the TV output won’t be parsed by this function (it will just return the value in the database regardless of output widgets and whatnot, so the function name is rather misleading). That may not matter to you in this circumstance, but if it does you’ll need to use a different method. One thing that you could try if you need a parsed TV value is passing it as a snippet parameter (e.g., [[mysnippet? &tvValue=`[*lang*]`]] ).
                  That’s actually not true ZAP; the getTemplateVarOutput() call should return the processed output for the document id you specify in the call, exactly as if you had called it using the tags on that document. The function name is completely accurate. What you describe is getTemplateVars(). In fact, I just fixed a bug related to this in the 0.9.6 release, that was not properly getting the value for @INHERIT bindings from other documents. If you are encountering other problems getting the proper output from this function, please let us know. wink

                  The approach of passing the TV in the snippet parameter may not work in some cases (I believe that was originally what was being attempted) , depending on the cacheability of the page, if it’s called with non-cacheable tags, i.e. [!snippet? &param=`[*lang*]`!], etc.; that’s all I was getting at. But after looking again at the issue, I just noticed the expectation of getting the snippet parameter from the $_GET array. In fact it would be extract()’d as an actual php variable, so the original code should have been:

                  if(isset($tvValue)) {$var=$tvValue;}


                  If you wanted to be able to override that TV’s value from the query string in a URL, you could look at the $_GET array...
                    • 19889
                    • 616 Posts
                    quick question: if I have something like this as the default value in a TV: @EVAL return $modx->documentIdentifier; what am I supposed to see on the page when editing the page - I currently see the same as entered as the default value but not the effective id of the document?

                    thanks again
                      • 33372
                      • 1,611 Posts
                      Quote from: OpenGeek at Jul 08, 2007, 11:43 PM

                      That’s actually not true ZAP; the getTemplateVarOutput() call should return the processed output for the document id you specify in the call, exactly as if you had called it using the tags on that document.
                      Weird. I just tried to use this in the way that I expected it not to work, and it did in fact work. Has this been fixed since 0.9.5, because I remember giving up on that function for parsed TV values around then?

                      To test this, I created a multi-select listbox TV ("test") and gave it a delimited list widget and placed it on a template as [*test*]. Then I created a snippet ("stest") with the following code in it and also placed it on the same template as [!stest!]:

                      $tv=$modx->getTemplateVarOutput(’test’, $modx->documentIdentifier);
                      return $tv[’test’];

                      To my surprise, both returned the same thing (the parsed TV value), which is exactly what the function is supposed to do.
                        "Things are not what they appear to be; nor are they otherwise." - Buddha

                        "Well, gee, Buddha - that wasn't very helpful..." - ZAP

                        Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options