We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22934
    • 13 Posts
    Hi,

    Im trying to get used the new xpdo way of doing things in revolution (which is awesome by the way) so i decided to write a simple snippet to create a kind of blog list (i know i could use ditto). I can get the list of documents no problem but im having problems getting hold of the template vars attached to each document, please can somebody point me in the right direction?

    Thanks, Paul

    This is what i have done so far:
    $c= $modx->newQuery('modResource', 'web');
    $c->select('id');
    $c->leftJoin('modTemplateVars','tv');
    $c->where(array(
    	'parent' => $news_page_id,
    	'published' => 1,
    	'context_key' => 'web',
    ));
    $c->sortby('pagetitle');
    
    if($article_limit!='') {
    $c->limit($article_limit);
    }
    $collection = $modx->getCollection('modResource', $c);
    • Quote from: pdiki at Aug 11, 2008, 11:32 AM

      Im trying to get used the new xpdo way of doing things in revolution (which is awesome by the way) so i decided to write a simple snippet to create a kind of blog list (i know i could use ditto). I can get the list of documents no problem but im having problems getting hold of the template vars attached to each document, please can somebody point me in the right direction?
      Hello Paul. First, there is no direct relationship between a modResource and a modTemplateVar; they are related by way of modTemplate. Also, just querying the modTemplateVar table is not sufficient to get the actual value for a specific resource, as it could be inherited or dynamically calculated from other variables or code.

      This would get you all the raw, unprocessed values for a specific modResource:
      <?php 
      $tvs= $resource->getMany('modTemplateVar');
      ?>


      However, to get the actual processed value of the template variable for the specific resource, you still need to either process() the modTemplateVar element or use the renderOutput() method. All Elements in Revolution extend the modElement class, providing the process() function which is used by the MODx parsing routine to get output from the element from the current resource:
      <?php
      $tvOut = array(); // capture output of TV's, processing them for the current resource
      foreach ($tvs as $tv) {
          $tvOut[$tv->name]= $tv->process();
      }
      ?>


      You can get the processed output for a TV when you are not on the resource by using the renderOutput() function directly:
      <?php
      $someResourceId = 20; // some arbitrary Resource id
      $tv = $modx->getObject('modTemplateVar', array('name' => 'foo'));
      if ($tv) {
          $foo = $tv->renderOutput($someResourceId);
      }
      ?>
        • 22934
        • 13 Posts
        Thanks Jason,

        That makes a whole load more sense now! And i got it working, cheers!

        Paul
          • 22934
          • 13 Posts
          Hi Jason,

          I got this working for some simple stuff and for a more complex example but not in a very clean way! Im trying to clean it up abit. Basically i have container of documents each using the same template, this template has a date template variable assigned to it. Im trying to get the collection of documents and sort them by the date template variable in one query, is this possible?

          Thanks Paul
          • Quote from: pdiki at Aug 19, 2008, 05:05 PM

            Hi Jason,

            I got this working for some simple stuff and for a more complex example but not in a very clean way! Im trying to clean it up abit. Basically i have container of documents each using the same template, this template has a date template variable assigned to it. Im trying to get the collection of documents and sort them by the date template variable in one query, is this possible?

            Thanks Paul
            You could but it would probably be more efficient to pull all the results back with the date value from the TV joined, then sort the array, unless you are working with a very large number of documents in these containers.