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.

Blog Module /

Discuss the Blog Module.

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

Pagination revamp "/blog/page/2"


Go to End


2 Posts   1552 Views

Avatar
jaredkipe

Community Member, 16 Posts

15 July 2011 at 12:27pm

Edited: 15/07/2011 12:44pm

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.

Avatar
jaredkipe

Community Member, 16 Posts

16 July 2011 at 12:07pm

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 %>