Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Count Albums

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 1905 Views
  • redactuk
    Avatar
    Community Member
    115 Posts

    Count Albums Link to this post

    In my templates is there a default way to test how many albums there are in a gallery?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Count Albums Link to this post

    $Albums.Count doesn't work?

  • redactuk
    Avatar
    Community Member
    115 Posts

    Re: Count Albums Link to this post

    What I'm trying to do on my website menu is test to to see if a gallery (I have many) has more than one album:

          <ul class="topNav">
             <li class="top p3"><a id="clients" class="top_link" href="/gallery">Gallery</a>
                <% if ChildrenOf(gallery) %>
                <ul class="sub">
                <% control ChildrenOf(gallery) %>
                 <li><a href="$Link" class="fly">$MenuTitle</a>
                   <% if Albums %>
                      <ul>
                         <% control Albums %>
                            <% if IsNotDefaultAlbum %>
                               <li><a href="$Link">$AlbumName</a></li>
                          <% end_if %>
                          <% end_control %>
                      </ul>
                   <% end_if %>
                 </li>
                <% end_control %>
                </ul>
                <% end_if %>
             </li>
          </ul>

    So ideally I need to replace the <% if Albums %> with an "if albums > 1"

    The reason for this is I need to prevent a list being created where there is just one album, because where there is just one album this is NOT displayed as a result of my <% if IsNotDefaultAlbum %>

  • UncleCheese
    Avatar
    4085 Posts

    Re: Count Albums Link to this post

    I'm not sure that you can do a greater/less than comparison on the template level, unfortunately.

    I would probably do:

    <% if Albums.Count =1 %><% else %>Albums is greater than one<% end_if %>

    Kind of cheesy, but since Albums.Count cannot equal zero, it should work.

  • redactuk
    Avatar
    Community Member
    115 Posts

    Re: Count Albums Link to this post

    Tried

    <% if Albums.Count = 1 %>

    or as I think it should be?

    <% if Albums.Count == 1 %>

    but it just reports error.

    I've tested $Albums.Count and it is correctly evaluating, so why is this condition not being accepted?

  • redactuk
    Avatar
    Community Member
    115 Posts

    Re: Count Albums Link to this post

    Anyone?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Count Albums Link to this post

    Because SSViewer sucks sometimes. Often times, you have exactly the correct syntax, but for whatever reason, the templating engine was never coded to understand certain combinations of code, e.g.

    $Object.Property.Function(arg) never works

    So it would seem that SSViewer can only evaluate <% if Property = Value %> and not <% if Object.Property = value %>

    You might be inclined to just jump into a control, to avoid the dot syntax:

    <% control Albums %>
    <% if Count =1 %>

    But of course, a control on a DataObjectSet puts you in a loop, so that won't work.

    Fortunately for you, there is a method on ImageGalleryPage called SingleAlbumView(), which returns true if the gallery has only one album.

    <% control SomeGalleryPage %>
    <% if SingleAlbumView %>single album<% end_if %>

    I've also added a method in the latest rev for AlbumCount(), so you should be able to do <% if AlbumCount = 3 %> in a ImageGalleryPage control.

  • redactuk
    Avatar
    Community Member
    115 Posts

    Re: Count Albums Link to this post

    <% control SomeGalleryPage %>
    <% if SingleAlbumView %>single album<% end_if %>

    That solves my problem - thanks UC. I've made note to replace it with AlbumCount on later maintenance update.

    1905 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.