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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

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

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

if Pos < 5 - this doesn't work


Go to End


5 Posts   7773 Views

Avatar
darkhouse

Community Member, 12 Posts

10 July 2008 at 4:45pm

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"?

Avatar
Sean

Forum Moderator, 922 Posts

10 July 2008 at 8:01pm

Edited: 10/07/2008 8:13pm

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.

Avatar
darkhouse

Community Member, 12 Posts

10 July 2008 at 9:05pm

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?

Avatar
Willr

Forum Moderator, 5523 Posts

10 July 2008 at 10:14pm

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;
}

Avatar
bunheng

Community Member, 78 Posts

27 August 2011 at 2:52pm

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