Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » if Pos < 5 - this doesn't work

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 4059 Views
  • darkhouse
    Avatar
    Community Member
    12 Posts

    if Pos < 5 - this doesn't work Link to this post

    Is there any way to do this? I want just the first 4 children of a section, so I figured I could write

    <% if Pos < 5 %>

    but it fails, blank screen. It seems like the problem is the "less than" operator since "equals" works fine. Does SS not support "less than" or "greater than"?

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: if Pos < 5 - this doesn't work Link to this post

    No, it doesn't support what you're doing.

    The best way to solve what you're doing is to create a custom function for your <% control block, and filter the results using the limit argument to DataObject::get()

    e.g.

    class Page extends SiteTree {

    ...

    function ChildrenWithLimit($limit = 4) {
    return DataObject::get('Page', "ParentID = $this->ID", '', '', $limit);
    }

    ...

    }

    Then, you can use <% control ChildrenWithLimit %> in your template. Should you want to override the default of 4, you can use <% control ChildrenWithLimit(8) %>, for example.

  • darkhouse
    Avatar
    Community Member
    12 Posts

    Re: if Pos < 5 - this doesn't work Link to this post

    thanks, that sounds great, except the children I'm trying to pull in aren't children of the page I'm on. I was using ChildrenOf(page-url). How would I use that in the get function?

  • Willr
    Avatar
    Forum Moderator
    5502 Posts

    Re: if Pos < 5 - this doesn't work Link to this post

    Considering you are hard coding the url in the theme then you could hard code this in the code and use the ParentID column in the database and compare it against the ID of the pages url

    function ChildrenWithLimit($limit = 4) {
    $Parent = DataObject::get_one("Page", "URLSegment = 'page-url'");
    return ($Parent) ? $Children = DataObject::get("Page", "ParentID = '$Parent->ID'","","", $limit) : false;
    }

  • bunheng
    Avatar
    Community Member
    78 Posts

    Re: if Pos < 5 - this doesn't work Link to this post

    Hi,

    I finding the way to display record from dataobject by escape first top three records. Does any one have any idea?
    I am really appreciated your reply.

    Thanks
    Bunheng

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