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

    I have the following TVs:
    gallery_1_img, gallery_1_txt, gallery_2_img, gallery_2_txt, etc... (1 through 10)

    I need to do a loop through all those TVs and check if they have been filled out. If a TV has been filled out I need to output the value. I’m thinking i need to create a snippet, but that’s about as much as I can figure out myself. Can anyone point me in the right direction.

    Any help is greatly appreciated
    Jonas
      • 26903
      • 1,336 Posts
      You could have a look at the getTemplateVars API function, I’ve never used it but it seems to return the contents of template variables associated with the template the current document is using, or if specified another one. The problem you might have is ’to check if they have been filled out’. Not sure how you’d do this safely, there is no ’content is valid’ flag or some such to test here.

      There’s also getTemplateVar and getTemplateVarOutput as well.

        Use MODx, or the cat gets it!
        • 4172
        • 5,888 Posts
        you can get this:
        http://modxcms.com/Docmanager-881.html

        and try it with something like that: (not tested)

        <?php
        $path='./assets/snippets/path_of_documentclass/';
        require_once($path.'document.class.inc.php');
        $docid=$modx->getDocumentIdentifier;
        $doc = new Document($docid);
        
        for ($i = 1; $i <= 10; $i++) {
        
        $tv='tvgallery_'.$i.'_img';
        $imgtv=$doc->Get($tv);
        
        $tv='tvgallery_'.$i.'_txt';
        $txttv=$doc->Get($tv);
        
        $output.=$i.':'.$imgtv.':'.$txttv.'<br/>';
        
        }
        
        return $output;
        ?>
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 14617
          • 47 Posts
          Hi Guys

          Thanks for your replies. With your help I made the following snippet which does the job:
          <?php
          $idnames = '';
          $output = '';
          for ($i=1; $i <= 10; $i++) { 
          	// Create array of needed TVs
          	$idnames[] = 'gallery_'.$i.'_img';
          	$idnames[] = 'gallery_'.$i.'_txt';
          }
          $values = $modx->getTemplateVarOutput($idnames); // Getting the values of the TVs
          
          $first = 1;
          for ($i=0; $i <= 9; $i++) { // Looping through the newly created array of values
          	if (file_exists($values['gallery_'.$i.'_img'])) {
          		// Only outputting if file_exists
          		$output .= '<a '.($first ? '' : 'style="display:none;"').' href="'.$values['gallery_'.$i.'_img'].'" rel="lightbox[gallery]" title="'.$values['gallery_'.$i.'_txt'].'">View Gallery</a>';
          		$first = 0;
          	}
          }
          
          return $output;
          ?>
          


          I tried using setPlaceholder and getChunk to make the output work from chunks, but I had problems working with those within a loop...