We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4906
    • 9 Posts
    I know snippets are for PHP and chunks for HTML. I also have come to the conclusion PHP isn't allowed within templates unless it's from a snippet, correct? I have this code I need to execute but it has a mixture of both. Where/how would I implement this since it has many php calls within html? I don't want to create a snippet for each call sad

    <div id="gallery">
     <?php foreach ($ph_sets['photoset'] as $ph_set): ?>
      <div class="photosets">
      <p><h2><?php print $ph_set['title']; ?></h2></p>
      <?php $photoset_id = $ph_set['id'];
      $photos = $f->photosets_getPhotos($photoset_id);
      foreach ($photos['photoset']['photo'] as $photo): ?>
      <div class="photos">
       <a rel="lightbox[]"
         href="<?= $f->buildPhotoURL($photo, 'medium') ?>"
         title="<?= $photo['title'] ?>">
       <img src="<?= $f->buildPhotoURL($photo, 'square') ?>"
         alt="<?= $photo['title'] ?>" title="<?= $photo['title'] ?>" />
       </a>
      </div>
      <?php endforeach; ?>
      </div>
     <?php endforeach; ?>
    </div>
      • 3749
      • 24,544 Posts


      The quick and dirty method would be something like this (untested):

      <?php
      $output = '<div id="gallery">';
      foreach ($ph_sets['photoset'] as $ph_set) {
        $output .= '<div class="photosets">
        <p><h2>' . $ph_set['title'] . '</h2></p>';
        $photoset_id = $ph_set['id'];
        $photos = $f->photosets_getPhotos($photoset_id);
        foreach ($photos['photoset']['photo'] as $photo) {
        $output .= '<div class="photos">
         <a rel="lightbox[]"
           href="' . $f->buildPhotoURL($photo, 'medium') . '" 
           title="' . $photo['title'] . '"
         <img src="' . $f->buildPhotoURL($photo, 'square') . '"' .
           'alt="' . $photo['title'] . '" title="' . $photo['title'] . '" />
         </a>
        </div>';
        }
        
        $output .= '</div>';
       }
      $output .= '</div>';
      
      return $output;


      I'll post a more proper method in a little while.


      ---------------------------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
      MODX info for everyone: http://bobsguides.com/modx.html
        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
        • 3749
        • 24,544 Posts
        I kind of threw this together, so it's almost certainly not correct, but it ought to get you started.


        The more proper method would be to create three HTML chunks with
        placeholders for the content: GalleryChunk and PhotoSetChunk, and PhotoChunk.
        and do something like this in the snippet:

        /* GalleryChunk:

        <div id="gallery">
           [[+sg.PhotoSets]]
        </div>
        


        PhotoSetChunk:

            <div class="photosets">
                  <h2>[[+sg.PhotoSetTitle]]</h2>
                  [[+sg.Photos]]
            </div>
        

        Photochunk:

                <div class="photos">
                      <a rel="lightbox[]"
                       href="[[+sg.PhotoUrlMedium]]" 
                       title="sg.PhotoTitle >"
                      <img src="[[+sg.PhotoUrlSquare]]"
                       'alt="[[+sg.PhotoTitle]]"
                       title="[[+sg.PhotoTitle]]" />
                       </a>
                </div>    
        



        [[ShowGallery? &galleryTpl=`GalleryChunk` &photoSetTpl=`PhotoSetChunk &photoTpl=`PhotoChunk`]]
        

        <?php
        /* ShowGallery snippet */
        
        /* Get the Tpl chunks */
        $outerTpl = $scriptProperties['GalleryChunk'];
        if (empty($outerTpl)) {
            return 'Could not find ' . $scriptProperties['PhotoChunk'] . ' chunk.';
        }
        
        $photoSetTpl = $scriptProperties['PhotoSetChunk'];
        if (empty($photoSetTpl)) {
            return 'Could not find ' . $scriptProperties['PhotoSetChunk'] . ' chunk.';
        }
        
        $photoTpl = $scriptProperties['myPhotoTpl'];
        
        if (empty($outerTpl)) {
            return 'Could not find ' . $scriptProperties['myPhotoTpl'] . ' chunk.';
        }
        
        $galleryContent = "";
        foreach ($ph_sets['photoset'] as $ph_set) {
            $innerContent = "";
            $photoset_id = $ph_set['id'];
            $photos = $f->photosets_getPhotos($photoset_id);
            foreach ($photos['photoset']['photo'] as $photo) {
                $fields = array();
                $fields['sg.PhotoUrlMedium'] = $f->buildPhotoUrl($photo, 'medium');
                $fields['sg.PhotoTitle'] = $photo['title'];
                $fields['sg.PhotoUrlSquary'] = $f->buildPhotoUrl($photo, 'medium');
                $fields['sg.alt'] = $photo['title'];
                $fields['sg.title'] = $photo['title'];
                $innerContent .= $modx->getChunk($photoTpl, $fields);
            }
            $ph['sg.PhotoSetTitle'] = $ph_set['title'];
            $ph['sg.Photos'] = $innerContent;
            $galleryContent .= $modx->getChunk($photoSetTpl, $ph);
            
        }
        
        $output = str_replace('[[+sg.PhotoSets]]', $galleryContent, $outerTpl);
        
        return $output;



        ---------------------------------------------------------------------------------------------------------------
        PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
        MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 14 years, 8 months ago.]
          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