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

    I am going through all my snippets and updating the deprecated functions. So far it has been pretty straightforward except for one little hangup:

    In one of my snippets I have a call to the deprecated getTemplateVarOutput method like this:

    $vars = $modx->getTemplateVarOutput(array('String'), $id);

    The legacy code summary says "use modTemplateVar->renderOutput() on a collection of modTemplateVar objects" but I'm not sure how to implement this in my context.

    Could anybody help me out?

    Much appreciated.
      • 3749
      • 24,544 Posts
      I'm not completely clear on what you're trying to do, but this might be what you want:

      $tv = $modx->getObject('modTemplateVar', array('name' => 'String'));
         $var = $tv->renderOutput($id);
      }
      


      If you just want the value for the current resource, you could do this:

      $val = $modx->resource->getTVValue('String');



        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
        • 42682
        • 20 Posts
        I am trying to execute a foreach loop to populate an image gallery with descriptions and links to each item's page. What I currently have is this:

        $children = $modx->getActiveChildren($modx->documentIdentifier, 'menuindex', 'ASC', 'id, pagetitle, introtext, hidemenu');
        $i = 0;
        foreach($children as $child) {
        if (!empty($child['hidemenu']))
        continue;
        $vars = $modx->getTemplateVarOutput(array('ProductImage'), $child['id']);

        <div class="group">
        <div class="box">
        <a href="[[~<?php echo $child['id'] ?>]]"><img src="<?php echo $vars['Image']; ?>" alt="<?php echo $child['pagetitle']; ?>"/></a>
        </div>
        <h4><?php echo $child['pagetitle']; ?></h4>
        <?php echo $child['introtext']; ?>


        <a href="[[~<?php echo $child['id'] ?>]]">Link Text</a>
        </div>

        I keep getting an error that the argument for the foreach is invalid.

        Could you please advise me of any possible workarounds for the deprecated getActiveChildren and getTemplateVarOutput methods?
          • 3749
          • 24,544 Posts
          Here's my best guess at what you want:

          A Tpl chunk called ImageListTpl:

          <div class="group">
              <div class="box">
                  <a href="[[~[[+id]]]]"><img src="[[+imgSrc]]" alt="[[+pagetitle]]"/></a>
              </div>
              <h4>[[+pagetitle]]</h4>
             <p>[[+introtext]]</p>
          
              <a href="[[~[[+id]]]]">Link Text</a>
          </div>


          And this in the snippet:

          <?php
          /* @var $modx modX */
          
          $c = array(
              'hidemenu' => 0,
              'published' => 1,
          );
          $output = '';
          $children = $modx->resource->getMany('Children', $c);
          
          foreach($children as $child) {
              /* @var $child modResource */
              $fields = $child->toArray();
              $fields['imgSource'] = $child->getTVValue('ProductImage');
              $output .= $modx->getChunk('ImageListTpl', $fields);
          }
          return $output;



          BTW, it's hard to believe that your original snippet code ran in any version of MODX.
            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
            • 42682
            • 20 Posts
            Thank you very much. That worked great!

            I have another instance where I am trying to work around some old functions in a similar way without the foreach loop.

            I would like to simply display a group of three product images that link to the specific page for that product.

            I would have a chunk that looks like this (somewhat similar to the one you listed above):

            <div class="group">
                <div class="box">
                    <a href="[[~[[+id]]]]"><img src="[[+imgSrc]]" alt="[[+pagetitle]]"/></a>
                </div>
                <h4> <a href="[[~[[+id]]]]">[[+pagetitle]]</a></h4>
             
            </div>
            


            The snippet I currently have looks like this:

            $i = 1;
            $product = 'product'. $i;
            while(!empty($$product) && $i < 99) {
               $child = $modx->getDocument($product);
               $vars = $modx->getTemplateVarOutput(array('ProductImage'), $child['id']);
               
              ?>
            <div class="group">
               <div class="box">
                  <a href="[[~<?php echo $child['id'] ?>]]"><img src="<?php echo $vars['ProductImage']; ?>" alt="<?php echo $child['pagetitle']; ?>"/></a>
               </div>
               <a href="[[~<?php echo $child['id'] ?>]]"><?php echo $child['pagetitle'] ?> »</a>
            </div>
               <?php
               if (++$i % 4 != 0)
                  echo '<div style="width: 13px; float: left;"> </div>';
               $product = 'product'. $i;
            }


            The call from the individual page from which this snippet would be imbedded looks like this:

            [[SnippetName? &product1=`{PageIDnumber}` &product2=`{PageIDnumber}` &product3=`{PageIDnumber}`]]



            What would be the best way to get around getDocument and getTemplateVarOutput in this context?

            Thank you again for your help.
              • 3749
              • 24,544 Posts
              Just do what we did above, but with something like this:


              $product = $scriptProperties['product' . $i];
              while(!empty($product) && $i < 99) {
                 $child = $modx->getObject('modResource', $product);
                 $tvValue = $child->getTVValue('ProductImage');
              
                  
                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
                • 42682
                • 20 Posts
                I'm a little confused as to how I should call the HTML to render this. Should I put it in an echo statement like in the bad snippet above or use a chunk like we did for the previous section we worked on? If I use a chunk how and where should I call it from? Inside the while loop?
                  • 3749
                  • 24,544 Posts
                  If I'm following you, you're getting and displaying multiple resources, so the getChunk() call would go inside the loop -- once for each resource, adding to the out put as you go and returning the results at the end (outside the loop). You definitely don't want echo statements or any <? php tags except the one at the very top.

                  $output .= '';
                  foreach(. . .) {
                  // . . .
                  $output .= $modx->getChunk(. . .);
                  }

                  return $output;
                    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
                    • 42682
                    • 20 Posts
                    The snippet is not rendering on the page so I must be misunderstanding what you are telling me to do.

                    Here's what I have...

                    In the chunk titled ThreeImgChunk:
                    <div class="group">
                        <div class="box">
                            <a href="[[~[[+id]]]]"><img src="[[+imgSrc]]" alt="[[+pagetitle]]"/></a>
                        </div>
                        <h4> <a href="[[~[[+id]]]]">[[+pagetitle]]</a></h4>
                    </div>
                    


                    for the snippet:

                    <?php
                    /* @var $modx modX */
                    
                    $c = array(
                        'hidemenu' => 0,
                        'published' => 1,
                    );
                    $output = '';
                    $children = $modx->resource->getMany('Children', $c);
                    
                    $i = 1; 
                     
                    foreach($children as $child) {
                        /* @var $child modResource */
                        $product = $scriptProperties['product' . $i];
                        while(!empty($product) && $i < 99) {
                       $child = $modx->getObject('modResource', $product);
                       $tvValue = $child->getTVValue('ProductImage');
                        $output .= $modx->getChunk('ThreeImgChunk', $tvValue);
                         if (++$i % 4 != 0)
                        echo '<div style="width: 13px; float: left;"> </div>';
                        $product = 'product'. $i;
                         }
                       
                       }
                    return $output;


                    and passing in three values from the resource page with this call:

                    [[RelatedProducts? &product1=`5` &product2=`10` &product3=`40`]]




                    I really appreciate your help with this. Thank you.
                      • 3749
                      • 24,544 Posts
                      Try the original snippet I posted with that chunk:

                      <?php
                      /* @var $modx modX */
                       
                      $c = array(
                          'hidemenu' => 0,
                          'published' => 1,
                      );
                      $output = '';
                      $children = $modx->resource->getMany('Children', $c);
                       
                      foreach($children as $child) {
                          /* @var $child modResource */
                          $fields = $child->toArray();
                          $fields['imgSource'] = $child->getTVValue('ProductImage');
                          $output .= $modx->getChunk('ImageListTpl', $fields);
                      }
                      return $output;


                      You don't need getObject(), since you already have all the children (which I assume are the products).

                      The second argument to getChunk() needs to be an associative array containing key/value pairs:

                      'placholdername' => 'value',


                      The toArray() call sets all the content fields (id, pagetitle, introtext, etc.) for the resource currently being processed and the line just below it adds the TV (from that page) to the array.

                        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