Last active
May 4, 2020 05:04
-
-
Save mehori/198d5a5b1f2b7f20057b493528ec5e26 to your computer and use it in GitHub Desktop.
Hugoで頭のいいページネーションを作成する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ $paginator := .Paginator }} | |
{{ $adjacent_links := 2 }} | |
{{ $max_links := (add (mul $adjacent_links 2) 1) }} | |
{{ $lower_limit := (add $adjacent_links 1) }} | |
{{ $upper_limit := (sub $paginator.TotalPages $adjacent_links) }} | |
{{ if gt $paginator.TotalPages 1 }} | |
<div class="page"> | |
<ul> | |
<!-- Goto First page. --> | |
{{ if ne $paginator.PageNumber 1 }} | |
<li class="pagination__item pagination__item--first"> | |
<a class="pagination__link pagination__link--first" href="{{ $paginator.First.URL }}"> | |
<i class="fa fa-angle-double-left"></i> | |
</a> | |
</li> | |
{{ end }} | |
<!-- Previous page. --> | |
{{ if $paginator.HasPrev }} | |
<li class="pagination__item pagination__item--previous"> | |
<a href="{{ $paginator.Prev.URL }}" class="pagination__link pagination__link--previous"> | |
<i class="fa fa-angle-left"></i></a> | |
</a> | |
</li> | |
{{ end }} | |
<!-- logic for calculating middle numbers --> | |
{{ range $paginator.Pagers }} | |
<!-- use Scratch to avoid scope error --> | |
{{ $.Scratch.Set "page_number_flag" false }} | |
<!-- if there is enough pages --> | |
{{ if gt $paginator.TotalPages $max_links }} | |
<!-- Lower limit pages. --> | |
<!-- If the user is on a page which is in the lower limit. --> | |
{{ if le $paginator.PageNumber $lower_limit }} | |
<!-- If the current loop page is less than max_links. --> | |
{{ if le .PageNumber $max_links }} | |
{{ $.Scratch.Set "page_number_flag" true }} | |
{{ end }} | |
<!-- Upper limit pages. --> | |
<!-- If the user is on a page which is in the upper limit. --> | |
{{ else if ge $paginator.PageNumber $upper_limit }} | |
<!-- If the current loop page is greater than total pages minus $max_links --> | |
{{ if gt .PageNumber (sub $paginator.TotalPages $max_links) }} | |
{{ $.Scratch.Set "page_number_flag" true }} | |
{{ end }} | |
<!-- Middle pages. --> | |
{{ else }} | |
{{ if and ( ge .PageNumber (sub $paginator.PageNumber $adjacent_links) ) ( le .PageNumber (add $paginator.PageNumber $adjacent_links) ) }} | |
{{ $.Scratch.Set "page_number_flag" true }} | |
{{ end }} | |
{{ end }} | |
<!-- Simple page numbers. --> | |
{{ else }} | |
{{ $.Scratch.Set "page_number_flag" true }} | |
{{ end }} | |
<!-- Output page numbers. --> | |
{{ if eq ($.Scratch.Get "page_number_flag") true }} | |
<li {{ if eq . $paginator }} class="active" {{ end }}> | |
<a href="{{ .URL }}"> | |
{{ .PageNumber }} | |
</a> | |
</li> | |
{{ end }} | |
<!-- end range --> | |
{{ end }} | |
<!-- Next page. --> | |
{{ if $paginator.HasNext }} | |
<li class="pagination__item pagination__item--next"> | |
<a href="{{ $paginator.Next.URL }}"> | |
<i class="fa fa-angle-right"></i> | |
</a> | |
</li> | |
{{ end }} | |
<!-- Last page. --> | |
{{ if ne $paginator.PageNumber $paginator.TotalPages }} | |
<li class="pagination__item pagination__item--last"> | |
<a href="{{ $paginator.Last.URL }}"> | |
<i class="fa fa-angle-double-right"></i> | |
</a> | |
</li> | |
{{ end }} | |
</ul> | |
</div> | |
{{ end }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment