Jump to:

23001 Posts in 11702 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » How to test for Children()

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 263 Views
  • tazzydemon
    Avatar
    Community Member
    114 Posts

    How to test for Children() Link to this post

    I have this function in a custom page_controller to work out how many "columns" of display items I have for css purposes. Its children with published children.

    The problem is that $child->Children() returns an arraylist and therefore effectively true even when there are no published items and the printing sub-loop returns null.

    As a result the count is wrong. I also use <% if Children %> in my templates.

    Is there an alternative function?

    public function columnCount(){
    $count = 0;
    if($this->Children()){
    foreach($this->Children() as $child){
    if($child->Children()){
    $count++;
    // following 4 lines just for test
    foreach($child->Children() as $mokopuna){
    print_r("Child name: ");
    print_r($mokopuna->URLSegment.' - '.$mokopuna->URLSegment);
    print_r("<BR>");
    }
    }
    print_r("<hr>");
    }
    };
    return $count;
    }

  • tazzydemon
    Avatar
    Community Member
    114 Posts

    Re: How to test for Children() Link to this post

    Doh, think I might have figured it out all my by own self...

    test for $child->Children()->count()

  • Mo
    Avatar
    Community Member
    485 Posts

    Re: How to test for Children() Link to this post

    Assuming you are using SS 3, Children should return a DataList.

    So, you should be able to use:

    if($this->Children()->exists()) {}

    Or (in a template):

    <% if $Children.exists %><% end_if %>

    Mo

  • IOTI
    Avatar
    Community Member
    187 Posts

    Re: How to test for Children() Link to this post

    Did you try using the built in Modulus template variable? Would probably be easier then creating your own function to do the counting. Maybe not applicable for what you are trying to do though.

  • tazzydemon
    Avatar
    Community Member
    114 Posts

    Re: How to test for Children() Link to this post

    Thanks.

    BTW I don't think you need the $ inside the <% %>

  • tazzydemon
    Avatar
    Community Member
    114 Posts

    Re: How to test for Children() Link to this post

    I've used modulus elsewhere. This was just to start off loops like <ul>.. etc.

    I also have to precount things to figure out responsive column sizes in the controller

  • Mo
    Avatar
    Community Member
    485 Posts

    Re: How to test for Children() Link to this post

    Hi Tazzydemon,

    You are right, $ isn't required inside <% %>, but last docs I say indicated that you should use $, so I have been training myself to use it, just incase variables without $ are depreciated in the near future.

    Also, not sure if this is useful, or if you are aware, but you should also be able to call count inside your template, EG:

    $Children.Count
    <% if $Children.Count = 1 %>

    Cheers,

    Mo

    263 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.