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

adding next and previous page


Go to End


7 Posts   3312 Views

Avatar
Nicolaas

Forum Moderator, 224 Posts

30 May 2007 at 9:26pm

Hi

I would like to add a next and previous page.... I am a completely newbie, but I am not giving up. I am kind of pissed though, because I can do that in PHP in five minutes.... Anyway.

I think this is best added to /sapphire/core/model/sitetree.php and would go along the ways of

public function getNextPage() {
return DataObject::get_one("SiteTree", "insert smart SQL here" );
}

I have the following questions....

a. can someone tell me how to write that function
b. what are the chances of making it part SS?

I am desperate - so any help greatly appreciated!

Nicolaas

Avatar
Sam

Administrator, 690 Posts

2 June 2007 at 6:24pm

Do this

$pages = DataObject::get("SiteTree", "ParentID = $this->ID AND Sort > $this->Sort", "Sort", "", 1)
if($pages) return $pages->First();

For prev

$pages = DataObject::get("SiteTree", "ParentID = $this->ID AND Sort > $this-<Sort", "Sort DESC", "", 1)
if($pages) return $pages->First();

Avatar
Nicolaas

Forum Moderator, 224 Posts

5 June 2007 at 12:19am

Hi Sam

Thanks for your help.

For those interested, this is what I used in the end (in the mysite/code/page.ss file) - sorry for all the site specific details...

public function NextPage() {
$pages = DataObject::get("SiteTree", "ParentID = $this->ParentID AND Sort > $this->Sort", "Sort", "", 1);
foreach($pages as $page) {
return '<a href="' . $page->Link() . '" id="next" title="next page: ' . Convert::raw2xml($page->Title) . '"><img src="/mysite/images/next.gif" alt="next page" width="14" height="30" /></a>';
}
}
public function PreviousPage() {
$pages = DataObject::get("SiteTree", "ParentID = $this->ParentID AND Sort < $this->Sort", "Sort DESC", "", 1);
foreach($pages as $page) {
return '<a href="' . $page->Link() . '" id="prev" title="previous page: ' . Convert::raw2xml($page->Title) . '"><img src="/mysite/images/prev.gif" alt="previous page" width="14" height="30" /></a>';
}
}

Hope that helps.

Cheers

Nicolaas

Avatar
Nicolaas

Forum Moderator, 224 Posts

5 June 2007 at 10:42am

just to add to that.

The links can be displayed by adding $NextPage or $PreviousPager to your template (e.g. mysite/templates/page.ss)

Hope that helps.

Nicolaas

Avatar
Sam

Administrator, 690 Posts

6 June 2007 at 9:47am

This is fine for your specific purpose, but for a more general NextPage() feature, I would just return the data object.

You can then put

<% control NextPage %>
<a href="$Link">$Title</a>
<% end_control %>

Or

<a href="$NextPage.Link">$NextPage.Title</a>

Into your template. This keeps more HTML in your template file, which is officially a good thing, particularly if you've got a separate person helping with design.

Avatar
Nicolaas

Forum Moderator, 224 Posts

6 June 2007 at 11:44pm

Thanks Sam - totally hear what you are saying about having the html in the template is this exactly what attracted me to SS. You guys have done a great job in this regard!

Avatar
pac

Community Member, 25 Posts

31 July 2008 at 5:02am

Edited: 31/07/2008 5:57am

this is great, thank you. should be implemented in core,

Thanks for your help, and if there are any freelance developpers available out there, feel free to contact me for module and specific developments.

running ss2.2.2