We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3232
    • 380 Posts
    I am new to Modx and am just getting a grasp of it’s logic.

    How would I go about accessing a TV in a snippet. I can’t seem to find any reference in the documentation
      • 33337
      • 3,975 Posts
      Zaigham (aka zi) Reply #2, 20 years ago
        Zaigham R - MODX Professional | Skype | Email | Twitter

        Digging the interwebs for #MODX gems and bringing it to you. modx.link
        • 3232
        • 380 Posts
        so if i understand this correctly...

        $something = getTemplateVar($tvName)

        $something would be equal to the $tvName variable in the current document?
          • 33372
          • 1,611 Posts
          Actually, I don’t think so. What you’ll get from that function is an array of TV elements. One of these is "value".

          You might be looking for getTemplateVarOutput, which returns the value(s) entered into a TV, e.g.:

          $size=$modx->getTemplateVarOutput('itemSize',$modx->documentIdentifier);


          However, there is an issue with the output of this function in 0.9.2.1. It works, but if your TV is a multi-listbox or checkboxes or anything that can hold multiple values they will all be returned in one string with no separator to help you to split them up. This is supposed to be fixed in 0.9.5, but I haven’t tried it there yet myself.

          In that case, I fall back to regular SQL queries, e.g.:

          $whichTV=5; // ID for size TV
          $sql="SELECT * FROM modx_site_tmplvar_contentvalues WHERE tmplvarid='".$whichTV."' AND contentid ='".$modx->documentIdentifier."'";
          $result=$modx->dbQuery($sql);
          $allvalues=$modx->fetchRow($result);
          $values=explode('||',$allvalues['value']);
          
          foreach($values as $value){
             $output.='<option value="'.$value.'">'.$value.'</option>';
          }


          It’s not pretty, but it works...

          If however this function is now working as expected in 0.9.5, that’s one more reason to upgrade!

          I definitely think that the documentation could explain how these API functions work better than it does now (and a couple of examples wouldn’t hurt).
            "Things are not what they appear to be; nor are they otherwise." - Buddha

            "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

            Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
            • 3232
            • 380 Posts
            zi/ZAP

            I want to thank you for taking the time out of your day to help me with this.

            I finally understand how this works.

            Thanks,
            Brian