7913 Posts in 1355 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » DataObjectManager: Conditionals for number of items
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 707 Views |
-
DataObjectManager: Conditionals for number of items

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 %>
-
Re: DataObjectManager: Conditionals for number of items

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.
-
Re: DataObjectManager: Conditionals for number of items

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 %>
-
Re: DataObjectManager: Conditionals for number of items

2 August 2010 at 4:38am Last edited: 2 August 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 %>
| 707 Views | ||
|
Page:
1
|
Go to Top |

