We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    That's definitely not going to work. wink

    I don't think this is a job for the If snippet, and it's kind of inefficient to run getResources (which is a little slow), even when there are no books to display.

    Do you have a full getResources tag that works for you to display what you want when there are books to show? Could you post the full tag here (inside code tags -- highlight the code and click on the <> in the toolbar)?
      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
      • 52133
      • 6 Posts
      garybiltmore Reply #12, 8 years ago
      Thanks Bob. The full code is below and it works just fine. Here's what I'm looking for: If &parents=`[[*artists and poets]]` is empty, I don't want "Editions" or article.thumbList1 to appear and vice versa for Paintings and the second <article>.

      <div class="artColumn col-md-8">
      
          <h3>Editions</h3>
      	   <article class="thumbList1">
      			    [[!getResources?
      			        &parents=`[[*artists and poets]]`
      			        &depth=`0`
      			        &tpl=`boundEditionRow`
      			        &includeContent=`1`
      		            &limit=`10`
      		            &includeTVs=`1`
      		            &processTVs=`1`
      		            &showHidden=`1`
      		            &sortby=`menuindex`
      		            &sortdir=`ASC`
      			    ]]
                 </article>
      
           <h3>Paintings</h3>
      		<article class="thumbList2">
      			    [[!getResources?
      			        &parents=`[[*single works]]`
      			        &depth=`0`	
      			        &tpl=`boundEditionRow`
      			        &includeContent=`1`
      		            &limit=`10`
      		            &includeTVs=`1`
      		            &processTVs=`1`
      		            &showHidden=`1`
      		            &sortby=`menuindex`
      		            &sortdir=`ASC`
      			    ]]
      		</article>
      </div>
      
        • 3749
        • 24,544 Posts
        You could do this with output modifiers, but it would be significantly slower since you'd have to run getResources twice for each search, once just to see if it returns anything. This is a little more complicated, but much more efficient (untested):

        Put these tags on the page:

        <div class="artColumn col-md-8">
        
        [[!GetStuff? 
           &parentTv=`artists and poets` 
           &heading=`Editions` 
           &class=`thumblist1`
        ]]
        
        [[!GetStuff? 
           &parentTv=`single works` 
           &heading=`Paintings` 
           &class=`thumblist2`
        ]]
        
        </div>


        Create a snippet called GetStuff with this code:

        <?php
        /* GetStuff snippet */
        
        $parentTv = $modx->getOption('parentTv', $scriptProperties, 'Missing Parent TV');
        $heading = $modx->getOption('heading', $scriptProperties, 'Missing Heading');
        $class = $modx->getOption('class', $scriptProperties, 'Missing class');
        
        $parentTvValue = $modx->resource->getTVValue($parentTv);
        
        $count = $modx->getCount('modResource', array('parent' => $parentTvValue));
        if (!$count) { /* nothing to show */
            return '';
        }
        
        $fields = array(
            'parents' => $parentTvValue,
            'depth' => '0',
            'tpl' => 'boundEditionRow',
            'includeContent' => '1',
            'limit' => '10',
            'includeTVs' => '1',
            'processTVs' => '1',
            'showHidden' => '1',
            'sortby' => 'menuindex',
            'sortdir' => 'ASC',
        );
        
        
        $output = "\n<h3>" . $heading . '</h3>' .
            '   <article class="' . $class . '">' . "\n";
        
        $output .= $modx->runSnippet('getResources', $fields);
        
        $output .= "\n    <article>\n";
        
        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
          • 52133
          • 6 Posts
          garybiltmore Reply #14, 8 years ago
          I mostly understand what's going on, in running getResources only once. I added the GetStuff in the template and created the GetStuff php snippet. But no change. It still show both h3's.
            • 3749
            • 24,544 Posts
            Did you remove any calls to getResources on the page?

            Did you clear the site cache and your browser cache?

            Do the container pages identified by the ID value stored in the TV have no children at all (no deleted, unpublished, or hidden pages, and no sub-container)? If there are any of those, the snippet will see that the parent has children and will go ahead and run getResources so you'll still see the HTML for the 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
              • 52133
              • 6 Posts
              garybiltmore Reply #16, 8 years ago
              I commented out the calls to the getResources code. Cleared all caches when I first tried your code. No, there are no children of any kind, at least I don't think so.

              Let me explain the template structure: I create a resource for an artist, then if their are single works for this artist, I create a container resource with the artist's name, and place the work in this container. Then I can link the container to the artist, and the artist to the container using the template variables -- artists and single works. If there are no editions (artists and poets), there is no container and no link between the artist and the non-existent container.

              Hop this is clear.
                • 3749
                • 24,544 Posts
                I think I may see the problem. I thought that the stuff you want to show was direct children of the pages named in the TVs. It sounds like it's grandchildren. Is that the case?
                  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
                • discuss.answer
                  • 52133
                  • 6 Posts
                  garybiltmore Reply #18, 8 years ago
                  I got it to work with a few tweaks of your code. Many thanks.

                  
                  <?php
                  /* GetStuff snippet */
                   
                  $parentTv = $modx->getOption('parentTv', $scriptProperties, 'Missing Parent TV');
                  $heading = $modx->getOption('heading', $scriptProperties, 'Missing Heading');
                  $class = $modx->getOption('class', $scriptProperties, 'Missing class');
                   
                  $parentTvValue = $modx->resource->getTVValue($parentTv);
                  
                  if (!$parentTvValue) { return ''; } /* nothing to show */
                  
                  $fields = array(
                      'parents' => $parentTvValue,
                      'depth' => '0',
                      'tpl' => 'boundEditionRow',
                      'includeContent' => '1',
                      'limit' => '20',
                      'includeTVs' => '1',
                      'processTVs' => '1',
                      'showHidden' => '1',
                      'sortby' => 'menuindex',
                      'sortdir' => 'ASC',
                  );
                  
                  $output = "\n<h3>" . $heading . '</h3>' .
                      '   <article class="' . $class . '">' . "\n";
                   
                  $output .= $modx->runSnippet('getResources', $fields);
                   
                  $output .= "\n    </article>\n";
                   
                  return $output;
                  
                    • 3749
                    • 24,544 Posts
                    I'm glad you got it working. smiley
                      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