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

    I have a document that has 15 TVs and I would like to get only 10 of them.

    Until know I have 2 solutions:

    - One that gets all of them
    - One that gets each one individually

    One that gets all of them
    <?php
    $id = $modx->resource->get('id');
    
    $collection = $modx->getCollection('modResource', array('id' =>$id));
    
    foreach ($collection as $resourceId => $resource) {
    	
    	$tvs = array();
    	$templateVars = &$resource->getMany('TemplateVars');
    	foreach ($templateVars as $tvId => $templateVar) {
            $tvs[$tvPrefix . $templateVar->get('name')] = !empty($processTVs) ? $templateVar->renderOutput($resource->get('id')) : $templateVar->get('value');
        }
        
    }
    
    print_r($tvs); 
    ?>

    This is a copy/paste from getResources. It works but note that even if I have one document I have no idea how to do it without a loop. I tried getOne but I think it’s for something else.
    Can anyone point to the some resources to learn how to do it myself?


    One that gets each one individually
    <?php
    $id = $modx->resource->get('id');
    
    $tv1 = $modx->getObject('modTemplateVar', array('name' =>'TV1'));
    $tv2 = $modx->getObject('modTemplateVar', array('name' =>'TV2'));
    
    ....and so on
    
    echo $tv1->getValue($id);
    echo $tv1->getValue($id);
    echo $tv1->getValue($id);
    
    ....and so on
    ?>

    This solution takes each TV individually.


    At this point I’m not sure which solution is the best. I guess I’m looking for an API that gets only what I need and some pointers on what solution is faster/recommended.


    Thank you



      • 22303 MODX Staff
      • 10,725 Posts
      I’m confused, you are trying to get the TVs from the current Resource, but those are retrieved automatically. Querying them again is completely unnecessary.
        • 5340
        • 1,624 Posts
        shocked

        I wonder what was I thinking. Anyway it was a good learning experience.


        Just in case

        $id = $modx->resource->get(’id’);

        becomes

        $id = $modx->resource->get(100);

        is there a way I can get a list of TVs with only 1 API?


        Thank you





          • 22303 MODX Staff
          • 10,725 Posts
          Something like this?
          <?php
          // assume resourceId is set, e.g. as a snippet property...
          $results = array();
          $tvs = $modx->getCollection('modTemplateVar', array("`name` IN ('foo', 'bar', 'abc', '123')"));
          foreach ($tvs as $tv) $results[$tv->get('name')] = (empty($processTVs) ? $tv->getValue($resourceId) : $tv->renderOutput($resourceId));
          // now do something with the $results, e.g. set them as placeholders or pass as properties to a chunk...
          ?>


          NOTE: BTW, $modx->resource->get(100) is not a valid expression as there is no field on the current resource named `100`. But I understood what you meant. smiley
            • 36782
            • 109 Posts
            oh brilliant! smiley
            Just what I was needing. I couldn't make one of my own.

            <edit>
            Just in case a newb like me stumbles on this, here's an example using Opengeeks code to render an image from a set of tv's into a chunk:

            first, a template chunk - Picture.tpl
            <div class="[[+imageClass]]">
                <img src="[[+image" alt="[[+imageAlt]]">
                [[+imageCaption:notempty=`
                <small class="image-caption">[[+imageCaption]]</small>
                `]]
            </div>
            

            Then the O.G.'s code from above w/ an addition:
            <?php
            // assume resourceId is set, e.g. as a snippet property...
            $results = array();
            $tvs = $modx->getCollection(
                'modTemplateVar', 
                array("`name` IN (
                    'image',
                    'imageClass',
                    'imageAlt',
                    'imageCaption'
                )")
            );
            foreach ($tvs as $tv) $results[$tv->get('name')] = (empty($processTVs) ? $tv->getValue($resourceId) : $tv->renderOutput($resourceId));
            
            // render to a chunk of goodness
            return $modx->getChunk('Picture.tpl',$results);
            


            Thanks, OpenGeek, you made my day smiley [ed. note: unsub777 last edited this post 14 years, 11 months ago.]