MarkupPagerNav

MarkupPagerNav generates pagination navigation markup for PageArray, PaginatedArray, and any other object implementing WirePaginatable

Most site code does not instantiate MarkupPagerNav directly. The common path is:

  1. Get a limited result set, usually with $pages->find("selector, limit=N").
  2. Render the items.
  3. Call $items->renderPager() or $items->renderPagination().
$items = $pages->find("template=blog-post, limit=10");

foreach($items as $item) {
    echo "<article><h2>$item->title</h2></article>";
}

echo $items->renderPagination();

limit=N is what makes a result paginated. It gives the returned PageArray pagination metadata such as getTotal(), getLimit(), and getStart(). Without a limit, ProcessWire returns the full result set and there is usually nothing for MarkupPagerNav to render.

Note that to use /page2/ style pagination URLs on a rendered page, the Template used by the rendering Page must have its allowPageNum property set to 1. In the admin this is in: Setup > Templates > your-template > URLs > Allow Page Numbers. When page numbers are not allowed, MarkupPagerNav falls back to query-string pagination like ?page=2.

PageArray and PaginatedArray

renderPager($options)

$items = $pages->find("template=blog-post, limit=10");
echo $items->renderPager([
    'numPageLinks' => 5,
    'listClass' => 'pagination',
]);

renderPager() delegates to MarkupPagerNav when the module is installed.

renderPagination($options)

Alias of renderPager().

echo $items->renderPagination([
    'baseUrl' => $page->url,
]);

getPaginationString($label, $usePageNum)

Returns text describing the current pagination position.

echo $items->getPaginationString('Items');      // Items 1 to 10 of 100
echo $items->getPaginationString('Page', true); // Page 1 of 10

echo $items->getPaginationString([
    'label' => 'Items',
    'zeroLabel' => 'No items found',
]);

For the full shared option reference used by renderPager() and renderPagination(), see wire/core/PageArray/pagination-options.md.

Direct MarkupPagerNav Usage

Use the module directly when you need to render pagination for a custom WirePaginatable object, or when you want direct access to pager state after render.

$items = $pages->find("template=blog-post, limit=10");

$pager = $modules->get('MarkupPagerNav');
echo $pager->render($items, [
    'numPageLinks' => 5,
    'listClass' => 'pagination',
]);

if($pager->isLastPage()) {
    // The rendered pagination was on the last page.
}

render(WirePaginatable $items, array $options = [])

Renders pagination markup. Returns an empty string when there is no pagination to render, such as when total items are less than or equal to the current page limit.

Customizing Output

Pass an options array to render(), renderPager(), or renderPagination() to override defaults. Only specify what you want to change.

echo $items->renderPagination([
    'numPageLinks' => 5,
    'listClass' => 'uk-pagination',
    'currentItemClass' => 'uk-active',
    'currentLinkMarkup' => "<span>{out}</span>",
    'separatorItemLabel' => '<span>&hellip;</span>',
    'separatorItemClass' => 'uk-disabled',
    'nextItemLabel' => '<i class="fa fa-angle-double-right"></i>',
    'previousItemLabel' => '<i class="fa fa-angle-double-left"></i>',
    'nextItemClass' => '',
    'previousItemClass' => '',
    'lastItemClass' => '',
]);
General Options
OptionDefaultDescription
numPageLinks10Number of page links shown. Values of 5 or more are recommended.
baseUrl''Base URL for pagination links. Auto-detected from the current page when empty.
getVars[]GET variables to append to pagination URLs.
pagenullCurrent Page, or null to auto-detect from $page.
arrayToCSVtrueConvert array GET values to CSV in URLs, such as ?tags=a,b. When false, array values use tags[]=a&tags[]=b style.

Pagination URLs are built from $input->whitelist() values automatically when getVars is empty. Use getVars when you need to include specific query-string parameters regardless of whitelist state. In either case, make sure variables are validated before including them in the pagination URLs.

Markup Options
OptionDefaultDescription
listMarkup<ul class='{class}' role='navigation' aria-label='{aria-label}'>{out}</ul>Container markup.
itemMarkup<li aria-label='{aria-label}' class='{class}' {attr}>{out}</li>Item markup.
linkMarkup<a href='{url}'><span>{out}</span></a>Link markup.
currentLinkMarkup<a href='{url}'><span>{out}</span></a>Current-page link markup.
separatorItemMarkupnullSeparator markup. Falls back to itemMarkup when null.

Tokens available in markup templates:

TokenAvailable inDescription
{out}all markup optionsItem content/label.
{url}linkMarkup, currentLinkMarkupLink href URL.
{class}itemMarkup, listMarkupCSS class attribute.
{aria-label}itemMarkup, listMarkupARIA label text.
{attr}itemMarkupExtra attributes, such as aria-current for the current page item.
Class Options
OptionDefaultDescription
listClass'MarkupPagerNav'CSS class for the list container.
currentItemClass'MarkupPagerNavOn'Current page item.
nextItemClass'MarkupPagerNavNext'Next button item.
previousItemClass'MarkupPagerNavPrevious'Previous button item.
firstItemClass'MarkupPagerNavFirst'First item in the rendered pager.
lastItemClass'MarkupPagerNavLast'Last item in the rendered pager.
firstNumberItemClass'MarkupPagerNavFirstNum'First numbered page item.
lastNumberItemClass'MarkupPagerNavLastNum'Last numbered page item.
separatorItemClass'MarkupPagerNavSeparator'Separator item.
Label and ARIA Options
OptionDefaultDescription
nextItemLabel'Next'Next button label.
previousItemLabel'Prev'Previous button label.
separatorItemLabel'&hellip;'Separator label.
listAriaLabel'Pagination links'List ARIA label.
itemAriaLabel'Page {n}'Page item ARIA label.
currentItemAriaLabel'Page {n}, current page'Current page ARIA label.
currentItemExtraAttraria-current='true'Extra attributes applied to the current page item.
nextItemAriaLabel'Next page'Next button ARIA label.
previousItemAriaLabel'Previous page'Previous button ARIA label.
lastItemAriaLabel'Page {n}, last page'Last page ARIA label.
Notes
  • MarkupPagerNav is not autoloaded; load it with $modules->get('MarkupPagerNav') when using it directly without a PaginatedArray or PageArray object.
  • PageArray and PaginatedArray are usually the best public API surface: $items->renderPager(), $items->renderPagination(), and $items->getPaginationString().
  • MarkupPageArray is an autoload module that hooks PaginatedArray::renderPager() and uses MarkupPagerNav internally.
  • render() updates $config->urls->next, $config->urls->prev, and $config->pagerHeadTags when next/previous pages exist.
  • Source file: wire/modules/Markup/MarkupPagerNav/MarkupPagerNav.module
API reference: methods, properties, hooks
// Get an instance of MarkupPagerNav
$pager = $modules->get('MarkupPagerNav'); 

This module can create pagination for a PageArray or any other kind of PaginatedArray type.Below is an example of creating pagination for a PageArray returned from $pages->find().

// $items can be PageArray or any other kind of PaginatedArray type
$items = $pages->find("id>0, limit=10"); // replace id>0 with your selector
if($items->count()) {
  $pager = $modules->get("MarkupPagerNav");
  echo "<ul>" . $items->each("<li>{title}</li>") . "</ul>";
  echo $pager->render($items); // render the pagination navigation
} else {
  echo "<p>Sorry there were no items found</p>";
}

Here’s a shortcut alternative that you can use for PageArray types (thanks to the MarkupPageArray module). Note that in this case, it’s not necessary to load the MarkupPagerNav module yourself:

$items = $pages->find("id>0, limit=10"); // replace id>0 with your selector
if($items->count()) {
  echo "<ul>" . $items->each("<li>{title}</li>") . "</ul>";
  echo $items->renderPager(); // render the pagination navigation
} else {
  echo "<p>Sorry there were no items found</p>";
}

It’s common to specify different markup and/or classes specific to the need when rendering pagination. This is done by providing an $options array to the MarkupPagerNav::render() call. In the example below, we'll specify Uikit markup rather then the default markup:

// Change options for Uikit "uk-pagination" navigation
$options = array(
  'numPageLinks' => 5,
  'listClass' => 'uk-pagination',
  'linkMarkup' => "<a href='{url}'>{out}</a>",
  'currentItemClass' => 'uk-active',
  'separatorItemLabel' => '<span>&hellip;</span>',
  'separatorItemClass' => 'uk-disabled',
  'currentLinkMarkup' => "<span>{out}</span>"
  'nextItemLabel' => '<i class="uk-icon-angle-double-right"></i>',
  'previousItemLabel' => '<i class="uk-icon-angle-double-left"></i>',
  'nextItemClass' => '', // blank out classes irrelevant to Uikit
  'previousItemClass' => '',
  'lastItemClass' => '',
);

$items = $pages->find("id>0, limit=10"); // replace id>0 with your selector

if($items->count()) {
  $pager = $modules->get('MarkupPagerNav');
  echo "<ul>" . $items->each("<li>{title}</li>") . "</ul>";
  echo $pager->render($items, $options); // provide the $options array
} else {
  echo "<p>Sorry there were no items found</p>";
}

The full list of options can be seen below. Please note that most options are set automatically since this module can determine most of the needed information directly from the WireArray that it’s given. As a result, it’s often not necessary to change any of the default options unless you want to change the markup and/or classes used in output.


Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the MarkupPagerNav class also inherits all the methods and properties of: Wire.

Show $var?     Show args?       Only hookable?    

General options

NameReturnSummary 
$pager->baseUrl string The base URL from which the navigation item links will start .
DEFAULT: ''
 
$pager->getVars array GET vars that should appear in the pagination, or leave empty and populate $input->whitelist (recommended).  
$pager->numPageLinks int The number of links that the pagination navigation should have, minimum 5 .
DEFAULT: 10
 
$pager->page null Page The current Page, or leave NULL to autodetect.  

Markup options

NameReturnSummary 
$pager->currentLinkMarkup string Link markup for current page. Place {url} for href attribute and {out} for label content. .
DEFAULT: "<a href='{url}'><span>{out}</span></a>"
 
$pager->itemMarkup string List item markup. Place {class} for item class (required), and {out} for item content. .
DEFAULT: "<li class='{class}' aria-label='{aria-label}'>{out}</li>"
 
$pager->linkMarkup string Link markup. Place {url} for href attribute, and {out} for label content. .
DEFAULT: "<a href='{url}'><span>{out}</span></a>"
 
$pager->listMarkup string List container markup. Place {out} where you want the individual items rendered and {class} where you want the list class .
DEFAULT: "<ul class='{class}' aria-label='{aria-label}'>{out}</ul>"
 
$pager->separatorItemMarkup string Markup to use for the "..." separator item, or NULL to use $itemMarkup .
DEFAULT: NULL
 

Class options

NameReturnSummary 
$pager->currentItemClass string Class for current item .
DEFAULT: 'MarkupPagerNavOn'
 
$pager->firstItemClass string Class for first item .
DEFAULT: 'MarkupPagerNavFirst'
 
$pager->firstNumberItemClass string Class for first numbered item .
DEFAULT: 'MarkupPagerNavFirstNum'
 
$pager->lastItemClass string Class for last item .
DEFAULT: 'MarkupPagerNavLast'
 
$pager->lastNumberItemClass string Class for last numbered item .
DEFAULT: 'MarkupPagerNavLastNum'
 
$pager->listClass string The class name to use in the $listMarkup .
DEFAULT: 'MarkupPageNav'
 
$pager->nextItemClass string Class for next item .
DEFAULT: 'MarkupPagerNavNext'
 
$pager->previousItemClass string Class for previous item .
DEFAULT: 'MarkupPagerNavPrevious'
 
$pager->separatorItemClass string Class for separator item .
DEFAULT: 'MarkupPagerNavSeparator'
 

Label options

NameReturnSummary 
$pager->currentItemAriaLabel string Label announcing current page to screen readers .
DEFAULT: 'Page {n}, current page'
 
$pager->itemAriaLabel string Label announcing page number to screen readers .
DEFAULT: 'Page {n}'
 
$pager->listAriaLabel string Label announcing pagination to screen readers .
DEFAULT: 'Pagination links'
 
$pager->nextItemLabel string label used for the 'Next' button .
DEFAULT: 'Next'
 
$pager->previousItemLabel string label used for the 'Previous' button .
DEFAULT: 'Prev'
 
$pager->separatorItemLabel string label used in the separator item .
DEFAULT: '…'
 

Other options

NameReturnSummary 
$pager->arrayToCSV bool When arrays are present in getVars, they will be translated to CSV strings in the queryString "?var=a,b,c". If set to false, then arrays will be kept in traditional format: "?var[]=a&var[]=b&var=c".
DEFAULT: true
 
$pager->itemsPerPage int Get number of items to display per page (set automatically, pulled from limit=n).  
$pager->pageNum int Get or set the current page number (1-based, set automatically).  
$pager->queryString string Get or set query string used in links (set automatically, based on $input->whitelist or getVars array).  
$pager->totalItems int Get total number of items to paginate (set automatically).  

Additional methods and properties

In addition to the methods and properties above, MarkupPagerNav also inherits the methods and properties of these classes:

API reference based on ProcessWire core version 3.0.267