Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

DataObjectManager: Conditionals for number of items


Reply


4 Posts   1005 Views

Avatar
DeklinKelly

Community Member, 197 Posts

2 August 2010 at 2:48am

How can I code conditional statements for the number of total items using the DataObjectManager module?

My code does NOT work but it demonstrates what I need:

   <% if GetSliderItems %>
<% control GetSliderItems %>
      Item Stuff Here
<% end_control %>

   <% if count(GetSliderItems) === 1 %> 1 Item <% end_if %>
   <% if count(GetSliderItems) === 2 %> 2 Items <% end_if %>
   <% if count(GetSliderItems) === 3 %> 3 Items <% end_if %>

   <% end_if %>

Avatar
UncleCheese

Forum Moderator, 4096 Posts

2 August 2010 at 3:40am

I'm wondering why your method is "GetSliderItems".. Shouldn't you just use <% control SliderItems %>, if your $has_many relation is "SliderItems"? Avoid using "Get" because it's going to cause confusion if ever use a custom getter.

$SliderItems.Count is what you want.

Avatar
DeklinKelly

Community Member, 197 Posts

2 August 2010 at 4:15am

Thank you for pointing out the significance of using "Get".

This does not work:

<% if SliderItems.Count === 1 %> Only 1 Item <% end_if %>

Avatar
UncleCheese

Forum Moderator, 4096 Posts

2 August 2010 at 4:38am

Edited: 02/08/2010 4:39am

First, you only need one = sign.

<% if SliderItems.Count = 1 %>

Second, that's a lot of syntax for an <% if %> block. I'm not sure SSViewer will parse that. You might have to use a custom function.

public function OneItem() {
return $this->SliderItems()->Count() == 1;
}

<% if OneItem %>