| 1 | {@ if (isset($totalPages) && $totalPages > 1) @} |
| 2 | <div class="pagination"> |
| 3 | {# 検索クエリがある場合はページネーションリンクに含める #} |
| 4 | {$ $queryParams = [] $} |
| 5 | {@ if (isset($_GET['q']) && !empty($_GET['q'])) @} |
| 6 | {$ $queryParams['q'] = $_GET['q'] $} |
| 7 | {@ endif @} |
| 8 | |
| 9 | {# 前のページへのリンク #} |
| 10 | {@ if (isset($currentPage) && $currentPage > 1) @} |
| 11 | {$ $prevParams = $queryParams $} |
| 12 | {$ $prevParams['page'] = $currentPage - 1 $} |
| 13 | {$ $prevQueryString = http_build_query($prevParams) $} |
| 14 | <a href="?{{ $prevQueryString }}" class="page-link">« 前</a> |
| 15 | {@ endif @} |
| 16 | |
| 17 | {# 表示するページ番号の範囲を計算(モバイル対応の為) #} |
| 18 | {# 最大表示ページ数 #} |
| 19 | {$ $rangeSize = 2 $} |
| 20 | {$ $startPage = max(1, $currentPage - floor($rangeSize / 2)) $} |
| 21 | {$ $endPage = min($totalPages, $startPage + $rangeSize - 1) $} |
| 22 | |
| 23 | {# 範囲の調整 #} |
| 24 | {@ if ($endPage - $startPage + 1 < $rangeSize && $startPage > 1) @} |
| 25 | {$ $startPage = max(1, $endPage - $rangeSize + 1) $} |
| 26 | {@ endif @} |
| 27 | |
| 28 | {# 最初のページへのリンク(多数のページがある場合) #} |
| 29 | {@ if ($startPage > 1) @} |
| 30 | {$ $firstParams = $queryParams $} |
| 31 | {$ $firstParams['page'] = 1 $} |
| 32 | {$ $firstQueryString = http_build_query($firstParams) $} |
| 33 | |
| 34 | <a href="?{{ $firstQueryString }}" class="page-link">1</a> |
| 35 | {@ if ($startPage > 2) @} |
| 36 | <span class="page-ellipsis">...</span> |
| 37 | {@ endif @} |
| 38 | {@ endif @} |
| 39 | |
| 40 | {@ for ($i = $startPage; $i <= $endPage; $i++) @} |
| 41 | {$ $pageParams = $queryParams $} |
| 42 | {$ $pageParams['page'] = $i $} |
| 43 | {$ $pageQueryString = http_build_query($pageParams) $} |
| 44 | |
| 45 | {@ if ($i == $currentPage) @} |
| 46 | <span class="page-current" aria-current="page">{{ $i }}</span> |
| 47 | {@ else @} |
| 48 | <a href="?{{ $pageQueryString }}" class="page-link">{{ $i }}</a> |
| 49 | {@ endif @} |
| 50 | {@ endfor @} |
| 51 | |
| 52 | {@ if ($endPage < $totalPages) @} |
| 53 | {# 最後のページへのリンク(多数のページがある場合) #} |
| 54 | {$ $lastParams = $queryParams $} |
| 55 | {$ $lastParams['page'] = $totalPages $} |
| 56 | {$ $lastQueryString = http_build_query($lastParams) $} |
| 57 | {@ if ($endPage < $totalPages - 1) @} |
| 58 | <span class="page-ellipsis">...</span> |
| 59 | {@ endif @} |
| 60 | <a href="?{{ $lastQueryString }}" class="page-link">{{ $totalPages }}</a> |
| 61 | {@ endif @} |
| 62 | |
| 63 | {# 次のページへのリンク #} |
| 64 | {@ if (isset($currentPage) && $currentPage < $totalPages) @} |
| 65 | {$ $nextParams = $queryParams $} |
| 66 | {$ $nextParams['page'] = $currentPage + 1 $} |
| 67 | {$ $nextQueryString = http_build_query($nextParams) $} |
| 68 | <a href="?{{ $nextQueryString }}" class="page-link">次 »</a> |
| 69 | {@ endif @} |
| 70 | </div> |
| 71 | {@ endif @} |