We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6059
    • 45 Posts
    How can i get the value of a tv how is filled with inherit from parent? In 2.0.7 i do it like this

    $tv = $modx->getTemplateVarOutput(array('tvname'), $element->get('id'));
         $languages = $tv['tvname'];


    But how do i this in 2.1??

    Thx for help
      • 22303 MODX Staff
      • 10,725 Posts
      Quote from: cschimanski at Jun 16, 2011, 08:49 AM

      How can i get the value of a tv how is filled with inherit from parent? In 2.0.7 i do it like this

      $tv = $modx->getTemplateVarOutput(array('tvname'), $element->get('id'));
           $languages = $tv['tvname'];


      But how do i this in 2.1??
      In MODX Revolution, you can get the processed value of a Template Variable for a specific Resource by doing the following:
      <?php
      $languages = '';
      $tv = $modx->getObject('modTemplateVar', array('name' => 'tvname'));
      if ($tv) {
          $languages = $tv->renderOutput($resourceId);
      }
      


      In MODX Revolution 2.1+, you can also use the new getElement() method available in the parser to take advantage of the new sourceCache when loading any MODX Element by name:
      <?php
      $languages = '';
      $tv = $modx->parser->getElement('modTemplateVar', 'tvname');
      if ($tv) {
          $languages = $tv->renderOutput($resourceId);
      }
      

      Using this approach improves performance, especially with non-cacheable Snippets, because it caches the source of the Element with the partial Resource cache, allowing cached accesses to the Resource to avoid going back to the database to grab the source of the TemplateVar or other Element. This only works if you are searching for the Element by name however.