diff options
Diffstat (limited to 'layouts/partials/pagination.html')
-rw-r--r-- | layouts/partials/pagination.html | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/layouts/partials/pagination.html b/layouts/partials/pagination.html new file mode 100644 index 0000000..4434645 --- /dev/null +++ b/layouts/partials/pagination.html @@ -0,0 +1,133 @@ +{{ $icons := (.Scratch.Get "svgBundle").RelPermalink }} + +{{ $pag := $.Paginator }} + + +<!-- + Blatantly copied from Glenn McComb + Source: https://glennmccomb.com/articles/how-to-build-custom-hugo-pagination/ +--> + +<!-- Number of links either side of the current page. Default value is 2 --> +{{ $adjacent_links := 1 }} + +<!-- $max_links = ($adjacent_links * 2) + 1 --> +{{ $max_links := (add (mul $adjacent_links 2) 1) }} + +<!-- $lower_limit = $adjacent_links + 1 --> +{{ $lower_limit := (add $adjacent_links 1) }} + +<!-- $upper_limit = $pag.TotalPages - $adjacent_links --> +{{ $upper_limit := (sub $pag.TotalPages $adjacent_links) }} + +<!-- If there's more than one page. --> +{{ if gt $pag.TotalPages 1 }} + + <nav> + <ul class="pagination"> + + <!-- First page. --> + {{ if ne $pag.PageNumber 1 }} + <li> + <a class="btn" href="{{ $pag.First.URL }}" aria-label="{{ T "go_to_first" }}"> + <svg aria-hidden="true"> + <use transform="rotate(180) translate(-18 -18)" xlink:href="{{ $icons }}#angle-double-right"/> + </svg> + </a> + </li> + {{ end }} + + <!-- Previous page. --> + {{ if $pag.HasPrev }} + <li> + <a class="btn" href="{{ $pag.Prev.URL }}" aria-label="{{ T "go_to_prev" }}"> + <svg transform="rotate(180)" aria-hidden="true"> + <use xlink:href="{{ $icons }}#angle-right"/> + </svg> + </a> + </li> + {{ end }} + + <!-- Page numbers. --> + {{ range $pag.Pagers }} + + {{ $.Scratch.Set "page_number_flag" false }} + + <!-- Advanced page numbers. --> + {{ if gt $pag.TotalPages $max_links }} + + <!-- Lower limit pages. --> + <!-- If the user is on a page which is in the lower limit. --> + {{ if le $pag.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 $pag.PageNumber $upper_limit }} + + <!-- If the current loop page is greater than total pages minus $max_links --> + {{ if gt .PageNumber (sub $pag.TotalPages $max_links) }} + {{ $.Scratch.Set "page_number_flag" true }} + {{ end }} + + + <!-- Middle pages. --> + {{ else }} + + {{ if and ( ge .PageNumber (sub $pag.PageNumber $adjacent_links) ) ( le .PageNumber (add $pag.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 . $pag }}class="current"{{ end }}> + {{ if eq . $pag }} + <p>{{ .PageNumber }}</p> + {{ else }} + <a class="btn" href="{{ .URL }}" aria-label="{{ T "go_to_pag" . }}" >{{ .PageNumber }}</a> + {{ end }} + </li> + {{ end }} + + {{ end }} + + <!-- Next page. --> + {{ if $pag.HasNext }} + <li> + <a class="btn" href="{{ $pag.Next.URL }}" aria-label="{{ T "go_to_next" }}"> + <svg aria-hidden="true"> + <use xlink:href="{{ $icons }}#angle-right"/> + </svg> + </a> + </li> + {{ end }} + + <!-- Last page. --> + {{ if ne $pag.PageNumber $pag.TotalPages }} + <li> + <a class="btn" href="{{ $pag.Last.URL }}" aria-label="{{ T "go_to_last" }}"> + <svg aria-hidden="true"> + <use xlink:href="{{ $icons }}#angle-double-right"/> + </svg> + </a> + </li> + {{ end }} + + </ul> + </nav> +{{ end }} |