I want to create a custom image gallery page

Silverstripe Version:

Question:

I want to create a custom image gallery page. All images are in thumbnail view with a title assigned to each one. I’m new here at silverstripe. I have a controller page and page.ss. Someone here has experience doing this. Thanks in advanced

// Include any relevant code. If you have a lot of code, link to a gist instead.

Hi there,

Please explain what specific help you’re after - we’re not going to write the functionality for you, but if you have a specific question or are stuck with a specific problem, we might be able to give you specific answers.

First, create a custom page type by creating a new class that extends the Page class and adding a GalleryImage data object to hold your gallery images. Here is an example of how you might set this up:

use SilverStripe\Assets\Image;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\HasManyList;
use SilverStripe\CMS\Model\SiteTree;

class GalleryPage extends SiteTree
{
  private static $db = [
    // Other fields go here
  ];

  private static $has_many = [
    'GalleryImages' => GalleryImage::class,
  ];
}

class GalleryImage extends DataObject
{
  private static $db = [
    'Caption' => 'Varchar',
  ];

  private static $has_one = [
    'Image' => Image::class,
    'GalleryPage' => GalleryPage::class,
  ];
}

Next, create a template for your gallery page. You can do this by creating a new file in the templates directory of your theme and giving it a name that matches the name of your custom page type (e.g. GalleryPage.ss).

In your template, you can use a loop to iterate over the GalleryImages for the current page and display them in your gallery. Here is an example of how you might do this:

<% loop GalleryImages %>
  <img src="$Image.URL" alt="$Caption" />
<% end_loop %>

This will display all of the images for the current gallery page in your template.