Jump to:

7940 Posts in 1543 Topics by 946 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, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 930 Views
  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    DataObjectManager: Conditionals for number of items Link to this post

    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 %>

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager: Conditionals for number of items Link to this post

    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.

  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Re: DataObjectManager: Conditionals for number of items Link to this post

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

    This does not work:

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

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager: Conditionals for number of items Link to this post

    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 %>

    930 Views
Page: 1
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.