28
Jun
7
Creating Previous and Next Buttons on a page
When you have a number of pages which follow on from one another you may find that asking a user to return to an index or select the next item from a side menu each time they want to progress is cumbersome. In this situation having previous and next buttons at the bottom of a page becomes very useful. In this snippet we'll add these buttons and also a counter to show the current page we are on and the total number of pages.
First add this function to your Page_Controller class:
Let's go through the function. First we pass in a string $Mode. This will be used to decide whether to return the previous or next page.
Then if we have passed in 'next' (the default) we setup the database criteria for that page, which is having the same parent ID and a sort value that is higher than the current page. We also set the database sort to arrange the pages by their Sort value in Ascending order. Otherwise if $Mode is 'prev' then we setup the criteria to fetch the previous page. If $Mode was not equal to either 'next' or 'prev' then we just return false.
Finally we do the database call and grab a single page using the criteria we set in the first part of the function and return the result.
Now we can use this function in our template like so:
By wrapping the links in <% control %> blocks instead of using $Variables, we prevent the link from being drawn if there is no page to go to.
Finally
we can also add these functions to our Page_Controller to enable us to
print the current page's number and the total number of pages:
And then in our template:
Thanks to Rick for suggesting this snippet!
About the Author
Name: Aram Balakjian
Website: http://www.aabweb.co.uk
Aram is a web designer/developer running London based agency aab web. He has a strong passion for developing attractive, usable sites around the SilverStripe CMS.
Comments (7)
-
Great Snippet!
In order to not display the link at all, I surrounded the link with <% if Title %>
<a...>
<% end_if %> (silverstripe 2.4)
Posted by Dirk Tannhaeuser, 15/07/2010 11:12am (14 days ago)
-
Hello and thank you for this nice tutorial.
Do you know some receipt how to simple can be done previous-next links for DOM item?
Posted by Maarika, 29/04/2010 11:19am (3 months ago)
-
Thanks!!!
Posted by Dustin, 01/04/2010 10:10am (4 months ago)
-
Thanks for this recipe, very useful as I try and get to grips with silverstripe!!
Posted by Mike, 17/03/2010 5:00am (4 months ago)
-
Thanks for this code also. I really appreciate the way you explain it. Thanks again ! You Wrooock ! ^^
Posted by Roxane, 22/02/2010 6:25am (5 months ago)
-
Hi Pete
Yea I did consider doing it this way, but it seemed cleaner to put them in a single function to prevent repetition, but there is no reason why these can't be seperated into two functions.Posted by aram, 28/06/2009 2:17pm (1 year ago)
-
I would much rather see <% control NextPage %> and <% control PrevPage %> rather than having to pass in a parameter, which also means that the back end functions are simpler.
Posted by Pete Bacon Darwin, 28/06/2009 6:06am (1 year ago)
RSS feed for comments on this page RSS feed for all comments