Helper: navigation/pager

« All helpers

Generates a reusable pager for lists of data.

Usage:

1. Set your data as follows:

$limit = 20;
$num = isset ($this->params[0])
    ? $this->params[0] // from the URL, e.g. /myapp/handler/#
    : 1;
$offset = ($num - 1) * $limit;

$items = MyModel::query ()->fetch ($limit, $offset);
$total = MyModel::query ()->count ();

$data = array (
    'limit' => $limit,
    'total' => $total,
    'items' => $items,
    'count' => count ($items),
    'single' => __ ('page'),
    'plural' => __ ('pages'),
    'url' => '/myapp/handler/%d'
);

echo $tpl->render ('myapp/view', $data);

2. In your template

Text pager:

{! navigation/pager
    ?style=text
    &url=[url]
    &total=[total]
    &count=[count]
    &limit=[limit] !}

Will show:

<< Newer results              Older results >>

Text pager with a custom label:

{! navigation/pager
    ?style=text
    &url=[url]
    &total=[total]
    &count=[count]
    &limit=[limit]
    &label=pages !}

Will show:

<< Newer pages              Older pages >>

Numeric pager:

{! navigation/pager
    ?style=numbers
    &url=[url]
    &total=[total]
    &count=[count]
    &limit=[limit] !}

Will show:

<< 1 2 3 4 >>

Numeric pager with extra links:

{! navigation/pager
    ?style=numbers
    &url=[url]
    &total=[total]
    &count=[count]
    &limit=[limit]
    &extra[all]=All pages
    &extra[/search]=Search !}

Will show:

<< 1 2 3 4 >> All pages Search

'All pages' will link to '/myapp/handler/all' based on the 'url' value in the PHP code above, while 'Search' will link to '/search', an external link.


Standard "X to Y of Z results" pager with a custom label:

{! navigation/pager
    ?style=results
    &url=[url]
    &total=[total]
    &count=[count]
    &limit=[limit]
    &single=page
    &plural=pages !}

Will show:

1 to 20 of 32 pages:

Short-form pager:

{! navigation/pager
    ?style=short
    &url=[url]
    &total=[total]
    &count=[count]
    &limit=[limit] !}

Will show:

1-20 of 32:

All elements can be styled with CSS classes.