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

    I'm new to modx and i've been trying to find out how i can pass a property from a snippet and use that property as the name of a TV.

    I have this in my snippet :

    for($i = 1;$i < 5; $i++){
       $images = $modx->resource->get('Images'.$i);
    
       $decoded = json_decode($images[1]);
       $imageLength = count($decoded);
       if($imageLength > 0){
       $properties = array('imageKey' => 'Images'.$i, 'contentKey' => 'Content'.$i);
       switch($imageLength){
          case ($imageLength < 2):
          $output = $modx->getChunk('SingleSection',$properties);
             return $output;
             break;
          case ($imageLength <= 6):
             $output = $modx->getChunk('SliderSection',$properties);
             return $output;
          break;
          default:
             $output = $modx->getChunk('GallerySection',$properties);
             return $output;
          break;
       }
       }
    }
    
    


    and this in my chunk which gets the relavent image TV (i.e images1, images2, images3 etc):

    <div class="imageSlider">
          <div class="imageStore store">
    	[[!getImageList? 
    		&tvname=`[[+imageKey]]`
    		&tpl=`@CODE:<div class="cell" style="background:url([[+image]]) no-repeat center center"></div>
    	`]]
    									
    	</div>
    </div>
    


    This works fine, but i also want to get a TV called content which is not part of migx. so i have content1, content2, content3 etc, which i'm passing to the chunk as a property. How do i use this to get the TV of the same name? eg.

    [[*Content1]]

    [[*Content2]]

    etc.

    Thanks for any help

      • 4172
      • 5,888 Posts
      what about preparing all the content inside your snippet:

      <?php
      
      for ($i = 1; $i < 5; $i++) {
          $images = $modx->resource->getTVValue('Images' . $i);
      
          $decoded = json_decode($images[1]);
          $imageLength = count($decoded);
          if ($imageLength > 0) {
      
              $gil_properties = array();
              $gil_properties['tvname'] = 'Images' . $i;
      
              switch ($imageLength) {
                  case ($imageLength < 2):
                      $gil_properties['tpl'] = '@CODE:<div class="cell" style="background:url([[+image]]) no-repeat center center"></div>';
                      $gil_output = $modx->runSnippet('getImageList', $gil_properties);
      
                      $properties = array('gil_output' => $gil_output, 'content' => $modx->resource->getTVValue('Content' . $i));
                      $output = $modx->getChunk('SingleSection', $properties);
                      return $output;
                      break;
                  case ($imageLength <= 6):
                      //different tpl goes here:
                      $gil_properties['tpl'] = '@CODE:<div class="cell" style="background:url([[+image]]) no-repeat center center"></div>';
                      $gil_output = $modx->runSnippet('getImageList', $gil_properties);
      
                      $properties = array('gil_output' => $gil_output, 'content' => $modx->resource->getTVValue('Content' . $i));
                      $output = $modx->getChunk('SliderSection', $properties);
                      return $output;
                      break;
                  default:
                      //different tpl goes here:
                      $gil_properties['tpl'] = '@CODE:<div class="cell" style="background:url([[+image]]) no-repeat center center"></div>';
                      $gil_output = $modx->runSnippet('getImageList', $gil_properties);
      
                      $properties = array('gil_output' => $gil_output, 'content' => $modx->resource->getTVValue('Content' . $i));
                      $output = $modx->getChunk('GallerySection', $properties);
                      return $output;
                      break;
              }
          }
      }
      


      and have chunks like that:

      <div class="imageSlider">
            <div class="imageStore store">
            [[+gil_output]]  
          </div>
          [[+content]]
      </div>


      But wouldn't it be the better way to have 5 sub-resources with one MIGX-TV and one content instead of 5 MIGX-TVs and 5 content-TVs at one resource?
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 47719
        • 11 Posts
        Thanks for taking the time to reply Bruno. The reason i didn't do the first solution was when i read the modx docs it seemed to suggest snippets should not contain any HTML to retain form, and html should just be in chunks. You should use the properties param to pass the chunk the values to the HTML. I'm a modx noob so i wouldn't be surprised if i'm wrong here or misunderstood what they meant.

        I thought of the sub resources but i thought the client would be more confused that way as they are basically adding psuedo pages.

        Thanks
          • 47719
          • 11 Posts
          Thanks for taking the time to reply Bruno. The reason i didn't do the first solution was when i read the modx docs it seemed to suggest snippets should not contain any HTML to retain form, and html should just be in chunks. You should use the properties param to pass the chunk the values to the HTML. I'm a modx noob so i wouldn't be surprised if i'm wrong here or misunderstood what they meant.

          I thought of the sub resources but i thought the client would be more confused that way as they are basically adding psuedo pages.

          Thanks
            • 4172
            • 5,888 Posts
            Improved version without any HTML inside:

            for ($i = 1; $i < 5; $i++) {
                $images = $modx->resource->getTVValue('Images' . $i);
            
                $decoded = json_decode($images[1]);
                $imageLength = count($decoded);
                $type = '';
                    
                if ($imageLength > 0) {
                    switch ($imageLength) {
                        case ($imageLength < 2):
                            $type = 'SingleSection';
                            break;
                        case ($imageLength <= 6):
                            $type = 'SliderSection';
                            break;
                        default:
                            $type = 'GallerySection';
                            break;
                    }
                    
                    $gil_properties = array();
                    $gil_properties['tvname'] = 'Images' . $i;
                    $gil_properties['tpl'] = $type . 'Tpl';
                    $gil_output = $modx->runSnippet('getImageList', $gil_properties);
            
                    $properties = array('gil_output' => $gil_output, 'content' => $modx->resource->getTVValue('Content' . $i));
                    $output = $modx->getChunk($type, $properties);
                    return $output;
            
                }
            }
            
            


            you need to create three new chunks then
            SingleSectionTpl
            SliderSectionTpl
            GallerySectionTpl
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 4172
              • 5,888 Posts

              I thought of the sub resources but i thought the client would be more confused that way as they are basically adding psuedo pages.

              It would also be possible to handle that 'pseudo-pages' within a MIGXdb - TV.
              In realitiy they are subpages, which the customer doesn't see.
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 47719
                • 11 Posts
                Thanks Bruno, i took your first reply and basically already modded it to your improved version. Cheers for all your help. Sorry about the double posts the forum keeps crashing while its posting.