Mecha CMS

Mecha CMS blog and documentation.

Class Navigator

Updated: Sunday, 07 August 2016

Pagination extractor for list of files.

Table of Content

Generate pagination from array input.

$offset = 1;
$per_page = 10;

$array = array(
    'item 1',
    'item 2',
    'item 3',
    …
    …
);

// Create pagination for the generated array chunks
$pager = Navigator::extract($array, $offset, $per_page, 'foo/bar');

foreach(Mecha::eat($array)->chunk($offset, $per_page)->vomit() as $item) {
    echo $item . '<br>';
}

echo $pager->prev->anchor . ' | ' . $pager->next->anchor;

Results for $pager:

$pager
 │
 ├── prev:
 │   ├── url: http://mecha-cms.com/2016/home
 │   └── anchor: ""
 ├── next:
 │   ├── url: http://mecha-cms.com/2016/foo/bar/2
 │   └── anchor: <a href="http://mecha-cms.com/2016/foo/bar/2" rel="next">Older</a>
 └── step:
     ├── url:
     │   ├── first: false
     │   ├── prev: false
     │   ├── 1: false
     │   ├── 2: http://mecha-cms.com/2016/foo/bar/2
     │   ├── 3: http://mecha-cms.com/2016/foo/bar/3
     │   ├── 4: http://mecha-cms.com/2016/foo/bar/4
     │   ├── 5: http://mecha-cms.com/2016/foo/bar/5
     │   ├── next: http://mecha-cms.com/2016/foo/bar/2
     │   └── last: http://mecha-cms.com/2016/foo/bar/5
     ├── anchor:
     │   ├── first: <span>First</span>
     │   ├── prev: <span>Previous</span>
     │   ├── 1: <strong class="current">1</strong>
     │   ├── 2: <a href="http://mecha-cms.com/2016/foo/bar/2">2</a>
     │   ├── 3: <a href="http://mecha-cms.com/2016/foo/bar/3">3</a>
     │   ├── 4: <a href="http://mecha-cms.com/2016/foo/bar/4">4</a>
     │   ├── 5: <a href="http://mecha-cms.com/2016/foo/bar/5">5</a>
     │   ├── next: <a href="http://mecha-cms.com/2016/foo/bar/2" rel="next">Next</a>
     │   └── last: <a href="http://mecha-cms.com/2016/foo/bar/5">Last</a>
     └── html: <span class="pagination"> … </span>

Options:

Navigator::extract($pages, $current, $per_page, $connector);
ParameterDescription
$pagesArray of files to be paginated.
$currentThe current page offset (for index page) or current file path (for single page).
$per_pageNumber of files to show per page request.
$connectorExtra path to be inserted into the generated pagination URL.

Hooks

Filter

FilterDescription
pager:urlWill affect the pagination URL.
pager:anchorWill affect the pagination anchor link.
pager:prev.urlWill affect the pagination URL of the previous navigation.
pager:next.urlWill affect the pagination URL of the next navigation.
pager:prev.anchorWill affect the pagination anchor link of the previous navigation.
pager:next.anchorWill affect the pagination anchor link of the next navigation.
pager:step.urlWill affect the numbered pagination URL.
pager:step.anchorWill affect the numbered pagination anchor link.
pager:step.htmlWill affect the generated HTML output of numbered pagination.

Weapon

None.

Donation and Email Subscription