Collections
Pagination
You can create a Blade template that displays your collection items in a paginated format by including a pagination key in the template’s YAML front matter. The pagination header should include the collection name and the perPage count:
posts.blade.php
---
pagination:
collection: posts
perPage: 5
---
@extends('_layouts.master')
...
If you don’t provide a
perPagevalue in your template, a default value can be set for specific collection by adding aperPagekey to the collection’s settings inconfig.php, or globally by adding aperPagekey to the top level ofconfig.php. Otherwise, the default value will be 10.
Once the pagination has been defined in the header, the template will have access to a special $pagination variable, which has several attributes:
$pagination->itemscontains an array of collection items for the current page$pagination->currentPagecontains the page number of the current page$pagination->totalPagescontains the total number of pages$pagination->pagescontains an array of paths to each page
Note that the
pagesare indexed by their page number, i.e. they are 1-based. So you can refer to the paths of a page by the page number, i.e.$pagination->page[1]will return the path to the first page.
$pagination->firstcontains the path to the first page (the same as$pagination->path[1])$pagination->lastcontains the path to the last page$pagination->nextcontains the path to the next page$pagination->previouscontains the path to the previous page
Using these $pagination attributes, you can build a set of pagination buttons and links:
@if ($previous = $pagination->previous)
<a href="{{ $page->baseUrl }}{{ $pagination->first }}"><<</a>
<a href="{{ $page->baseUrl }}{{ $previous }}"><</a>
@else
<< <
@endif
@foreach ($pagination->pages as $pageNumber => $path)
<a href="{{ $page->baseUrl }}{{ $path }}"
class="{{ $pagination->currentPage == $pageNumber ? 'selected' : '' }}">
{{ $pageNumber }}
</a>
@endforeach
@if ($next = $pagination->next)
<a href="{{ $page->baseUrl }}{{ $next }}">></a>
<a href="{{ $page->baseUrl }}{{ $pagination->last }}">>></a>
@else
> >>
@endif
To display the items on each page, iterate over the $pagination->items collection:
@foreach ($pagination->items as $post)
<h3><a href="{{ $post->getUrl() }}">{{ $post->title }}</a></h3>
<p class="text-sm">by {{ $post->author }} • {{ date('F j, Y', $post->date) }}</p>
<div>{!! $post->getContent() !!}</div>
@endforeach