Jump to:

1835 Posts in 1139 Topics by 554 members

Blog Module

SilverStripe Forums » Blog Module » Pagination revamp "/blog/page/2"

Discuss the Blog Module.

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

Page: 1
Go to End
Author Topic: 652 Views
  • jaredkipe
    Avatar
    Community Member
    16 Posts

    Pagination revamp "/blog/page/2" Link to this post

    I rewrote the pagination in my local Blog module to take the form of "/blog/page/2" instead of "/blog/?start=10". This is extremely similar to how the "tag/whatever" works. The biggest problem is how the BlogPagination.ss include file relies on DataObjectSet's NextLink and PevLink, which I didn't want to rewrite (and probably shouldn't because of where they might be used).

    in BlogTree.php -> BlogTree_Controller
    add 'page' to $allowed_actions
    modify inside method BlogEntries()

          $start = isset($_GET['start']) ? (int) $_GET['start'] : 0;


    to be

          if (isset($_GET['start'])) {
             $start = (int) $_GET['start'];
          } else {
             $start = (int) $this->SelectedPage();
             $start = $limit * ($start - 1);
          }

    Add these methods (preferably down by the SelectedTag()

       /** - Added 7/14/2011 by Jared Kipe
        * Return the currently viewing page, default is 1
        *
        * @return int
        */
       function SelectedPage() {
          return ($this->request->latestParam('Action') == 'page') ? max((int)$this->request->latestParam('ID'), 1) : 1;
       }
       function PreviousPage() {
          return $this->SelectedPage() - 1;
       }
       function NextPage() {
          return $this->SelectedPage() + 1;
       }
       // override for some parent's implementation of page that causes runtime error using the 'page' action
       function page() {
          return array();
       }

    This will take some modification to the BlogPagination.ss include file.

    <% if BlogEntries.MoreThanOnePage %>
       <div id="PageNumbers">
          <p>
             <% if BlogEntries.NotFirstPage %>
                <a class="prev" href="{$Link}<% if PreviousPage != 1 %>page/$PreviousPage<% end_if %>" title="View the previous page">Prev</a>
             <% end_if %>
          
             <span>
              <% control BlogEntries.PaginationSummary(4) %>
                   <% if CurrentBool %>
                      <span class="current">$PageNum</span>
                   <% else %>
                      <% if Link %>
                         <a href="{$Top.Link}page/$PageNum" title="View page number $PageNum">$PageNum</a>
                      <% else %>
                         &hellip;
                      <% end_if %>
                   <% end_if %>
                <% end_control %>
             </span>
          
             <% if BlogEntries.NotLastPage %>
                <a class="next" href="{$Link}page/$NextPage" title="View the next page">Next</a>
             <% end_if %>
          </p>
       </div>
    <% end_if %>

    EDIT: After thinking about it, this could probably cause problems when using tags or date, if there are are so many of either to cause pagination. Probably wrap the BlogPagination.ss file with a giant <% if SelectedTag %> or use two different includes based on SelectedTag. Allowing you to use the "old" BlogPagination.ss file with ?start=10 instead.

  • jaredkipe
    Avatar
    Community Member
    16 Posts

    Re: Pagination revamp "/blog/page/2" Link to this post

    As anticipated, the new BlogPagination.ss file does cause some problems if you are viewing a tag or date at the same time and there are so many as to include pagination.

    In both BlogTree.ss and BlogHolder.ss Layouts I conditionally included the original BlogPagination.ss file based on the presence of SelectedTag() and SelectedDate()

    Like so..

       <% if SelectedTag || SelectedDate %>
          <% include BlogPagination_orig %>
       <% else %>
          <% include BlogPagination %>
       <% end_if %>

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