In my templates is there a default way to test how many albums there are in a gallery?
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
DataObjectManager Module /
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Sean, Ed, biapar, Willr, Ingo, swaiba
Count Albums
- Page 11(current)
- 2
- Next 10 entries
$Albums.Count doesn't work?
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 %>
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.
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?
Anyone?
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.
<% 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.
- Page 11(current)
- 2
- Next 10 entries