Pagination

On overview pages, it is common to have multiple items. If you have a lot of items, that page will get dirty really fast. Because of that, we've provided an easy way to implement pagination.

Parameters

To use the pagination functionallity, we'll need to provide some parameters. First, we'll set some basic data.

protected $pagination = array(
	'limit' => 10,
	'offset' => 0,
	'requested_page' => 1,
	'num_items' => null,
	'num_pages' => null
);

Now it's time to get the data for the current page. To do this, we'll need the current page to calculate which item set we should get.

use Frontend\Core\Engine\Navigation as FrontendNavigation;
use Frontend\Core\Engine\Model as FrontendModel;
use Frontend\Modules\MiniBlog\Engine\Model as FrontendMiniBlogModel;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

...

// requested page
$requestedPage = $this->url->getParameter('page', 'int', 1);

$this->pagination['url'] = FrontendNavigation::getUrlForBlock('mini_blog');
$this->pagination['limit'] = FrontendModel::getModuleSetting('mini_blog', 'overview_num_items', 10);
$this->pagination['num_items'] = FrontendMiniBlogModel::getAllCount();
$this->pagination['num_pages'] = (int) ceil($this->pagination['num_items'] / $this->pagination['limit']);
$this->pagination['requested_page'] = $requestedPage;
$this->pagination['offset'] = ($this->pagination['requested_page'] * $this->pagination['limit']) - $this->pagination['limit'];

Let me explain what these parameters actually mean.

  • url: this will be used to navigate. Each page will use that specific url to add the ?page= variable to
  • limit: this is the limit of items you want on each page, this is usually set by a backend setting
  • num_items: this is a count of the items that are visible. Here you should validation on things like language, visible, published, etc.
  • num_pages: we could've done this automatically, but this is calculated here for a reason, wich you'll find out very soon requested_page: this is the variable that will be passed if you click on a new page (or next/previous)
  • offset: the start item, this is used to pass to our database so we can get the range we want

Now we've calculated all the required data for the pagination, let's validate if we have entered a page that actually exists.

if($this->pagination['num_pages'] == 0) $this->pagination['num_pages'] = 1;
if($requestedPage > $this->pagination['num_pages'] || $requestedPage < 1) {
     throw new NotFoundHttpException();
}

If we get past this, that means we're on a page that exists. Now all we have to do is fetch the data so we can assign it to our template.

$this->records = FrontendMiniBlogModel::getAll($this->pagination['limit'], $this->pagination['offset']);

Parsing

To parse the data to your template, all you have to do is assign it in you parse function and call it in your template.

Index.php

protected function parse()
{
	$this->template->assign('items', $this->records);
	$this->parsePagination();
}