We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • TemplateVariables are tricky as they have values and those values can contain MODx content tags, @bindings, etc. But you can do everything you need to with modTemplateVar objects, including getting the value, setting the value, and/or getting the processed output for a particular Resource:

    <?php
    $tv = $modx->getObject('modTemplateVar', array('name' => 'SomeTemplateVarOfMine'));
    if ($tv) {
        // get the unprocessed value for a resource
        $value = $tv->getValue($resource->get('id'));
        // get the processed output for a resource
        $output = $tv->renderOutput($resource->get('id'));
        // change the value for a resource
        $changed = $tv->setValue($resource->get('id'), 'new value');
    }
    ?>


    Just remember, the current Resource being rendered by MODx already has all of this data loaded automatically.
      • 1778
      • 659 Posts
      Thanks OpenGeek,

      this will be very helpful smiley

      cheers
        • 3749
        • 24,544 Posts
        Note that for the following code, you need to set $resource first either with $resource = $modx->getObject(’modResource’,$id) or, for the current resource: $resource = $modx->resource;
        Quote from: OpenGeek at Aug 23, 2009, 04:13 PM

        <?php
        $tv = $modx->getObject('modTemplateVar', array('name' => 'SomeTemplateVarOfMine'));
        if ($tv) {
            // get the unprocessed value for a resource
            $value = $tv->getValue($resource->get('id'));
            // get the processed output for a resource
            $output = $tv->renderOutput($resource->get('id'));
            // change the value for a resource
            $changed = $tv->setValue($resource->get('id'), 'new value');
        }
        ?>


          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 3749
          • 24,544 Posts

          // change the value for a resource
          $changed = $tv->setValue($resource->get('id'), 'new value');
          $tv->save();
          <br /><br />I can’t get this to have any effect. The original TV is unchanged and there’s nothing in the error log.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 1778
            • 659 Posts
            @BobRay : I have not tried this yet but maybe (not sure and just a noob suggestion)instead of $tv->save(), it would be more efficient to do $resource->save()... As far as I understand it, to save the document ($resource) seems to build the relationship to fill in the "site_tmplvar_contentvalues" table (document’s in ’contentid’ and the tv’s id in tmplvarid). Is this right ?
            And if I’m wrong, to set a tv value I have to build (or update if you want to change the value) the relationship (so how? smthg with addMany?) ... Really not sure of how it works...

            Cheers

            Sorry for my "hasardous" english...
            • Quote from: BobRay at Aug 24, 2009, 02:09 AM


              // change the value for a resource
              $changed = $tv->setValue($resource->get('id'), 'new value');
              $tv->save();
              <br /><br />I can’t get this to have any effect. The original TV is unchanged and there’s nothing in the error log.
              I’m sorry, what code are you using to define $resource or $tv? Some context would be useful as this code definitely works for me to set a value for a specific resource...
                • 3749
                • 24,544 Posts
                $resource = $modx->resource;


                I did clear MODx and browser caches.

                I also tried:

                $resource->save()

                and

                $tv->save();
                $resource->save();


                Maybe $modx->resource isn’t the equivalent of what you obtain with getObject(). It seems like it should be.
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                • Quote from: BobRay at Aug 24, 2009, 07:24 PM

                  $resource = $modx->resource;

                  ...

                  Maybe $modx->resource isn’t the equivalent of what you obtain with getObject(). It seems like it should be.
                  Be careful, that makes a new copy of the $modx->resource and will not reflect changes back to the original instance, though that is definitely not involved in this issue. $modx->resource always represents the currently requested Resource. You can get any resource back from getObject().

                  Quote from: BobRay at Aug 24, 2009, 07:24 PM

                  I also tried:

                  $resource->save()

                  and

                  $tv->save();
                  $resource->save();

                  $tv->save() is all that should be necessary after calling $tv->setValue(). I’m not sure what problem you are encountering...maybe some more context would help me visualize it? Sorry...
                    • 3749
                    • 24,544 Posts
                    $resource = $modx->resource;
                    
                    $tv = $modx->getObject('modTemplateVar', array('name' => 'BackgroundImage'));
                    
                    if ($tv) {
                        // get the processed output for a resource
                        $output = $tv->renderOutput($resource->get('id'));
                    
                        // change the value for a resource
                        $changed = $tv->setValue($resource->get('id'), 'new value');
                        $tv->save();
                        
                    } else {
                        $output = "TV Not Found";
                    }
                    
                    return $output;


                    That’s the whole snippet. I verified that the value is unchanged in the DB.

                    BackgroundImage is a TV with a default value of Blue, Input Type set to text, and Output Type set to default. The value of BackgroundImage in the resource is "xxxxxx" which never changes.

                    I tried using getObject() to set $resource, but, as you suggest, it made not difference.

                    I’m probably missing something dumb.
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 1778
                      • 659 Posts
                      I’ve tried it too... And no effect at all !
                      No value at all, neither in the document or in the table...
                      TV’s are drivin’ me crazy !