Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » adding next and previous page

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: 2751 Views
  • Nicolaas
    Avatar
    Forum Moderator
    213 Posts

    adding next and previous page Link to this post

    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

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: adding next and previous page Link to this post

    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();

  • Nicolaas
    Avatar
    Forum Moderator
    213 Posts

    Re: adding next and previous page Link to this post

    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

  • Nicolaas
    Avatar
    Forum Moderator
    213 Posts

    Re: adding next and previous page Link to this post

    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

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: adding next and previous page Link to this post

    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.

  • Nicolaas
    Avatar
    Forum Moderator
    213 Posts

    Re: adding next and previous page Link to this post

    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!

  • pac
    Avatar
    Community Member
    25 Posts

    Re: adding next and previous page Link to this post

    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

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