various fixes, added pagination and articles prev/next links.
[theme-danix.xyz.git] / layouts / partials / pagination.html
1 <!--
2 //
3 // PAGE NUMBERS
4 //––––––––––––––––––––––––––––––––––––––––––––––––––
5 // https://glennmccomb.com/articles/how-to-build-custom-hugo-pagination/
6 -->
7 {{ $paginator := .Paginator }}
8
9 <!-- Number of links either side of the current page. -->
10 {{ $adjacent_links := 2 }}
11
12 <!-- $max_links = ($adjacent_links * 2) + 1 -->
13 {{ $max_links := (add (mul $adjacent_links 2) 1) }}
14
15 <!-- $lower_limit = $adjacent_links + 1 -->
16 {{ $lower_limit := (add $adjacent_links 1) }}
17
18 <!-- $upper_limit = $paginator.TotalPages - $adjacent_links -->
19 {{ $upper_limit := (sub $paginator.TotalPages $adjacent_links) }}
20
21 <!-- If there's more than one page. -->
22 {{ if gt $paginator.TotalPages 1 }}
23
24 <ul class="pagination align-center">
25
26 <!-- First page. -->
27 {{ if ne $paginator.PageNumber 1 }}
28 <li>
29 <a class="page" href="{{ $paginator.First.URL }}">
30 <span class="button small">FIRST</span>
31 </a>
32 </li>
33 {{ end }}
34
35 <!-- Previous page. -->
36 {{ if $paginator.HasPrev }}
37 <li>
38 <a class="page" href="{{ $paginator.Prev.URL }}">
39 <span class="button small">PREV</span>
40 </a>
41 </li>
42 {{ end }}
43
44 <!-- Page numbers. -->
45 {{ range $paginator.Pagers }}
46
47 {{ $.Scratch.Set "page_number_flag" false }}
48
49
50 <!-- Advanced page numbers. -->
51 {{ if gt $paginator.TotalPages $max_links }}
52
53
54 <!-- Lower limit pages. -->
55 <!-- If the user is on a page which is in the lower limit. -->
56 {{ if le $paginator.PageNumber $lower_limit }}
57
58 <!-- If the current loop page is less than max_links. -->
59 {{ if le .PageNumber $max_links }}
60 {{ $.Scratch.Set "page_number_flag" true }}
61 {{ end }}
62
63
64 <!-- Upper limit pages. -->
65 <!-- If the user is on a page which is in the upper limit. -->
66 {{ else if ge $paginator.PageNumber $upper_limit }}
67
68 <!-- If the current loop page is greater than total pages minus $max_links -->
69 {{ if gt .PageNumber (sub $paginator.TotalPages $max_links) }}
70 {{ $.Scratch.Set "page_number_flag" true }}
71 {{ end }}
72
73
74 <!-- Middle pages. -->
75 {{ else }}
76
77 {{ if and ( ge .PageNumber (sub $paginator.PageNumber $adjacent_links) ) ( le .PageNumber (add $paginator.PageNumber $adjacent_links) ) }}
78 {{ $.Scratch.Set "page_number_flag" true }}
79 {{ end }}
80
81 {{ end }}
82
83
84 <!-- Simple page numbers. -->
85 {{ else }}
86
87 {{ $.Scratch.Set "page_number_flag" true }}
88
89 {{ end }}
90
91 <!-- Output page numbers. -->
92 {{ if eq ($.Scratch.Get "page_number_flag") true }}
93 <li>
94 <a href="{{ .URL }}" class="page {{ if eq . $paginator }} active{{ end }}">
95 {{ .PageNumber }}
96 </a>
97 </li>
98 {{ end }}
99
100 {{ end }}
101
102 <!-- Next page. -->
103 {{ if $paginator.HasNext }}
104 <li>
105 <a href="{{ $paginator.Next.URL }}" class="page">
106 <span class="button small">NEXT</span>
107 </a>
108 </li>
109 {{ end }}
110
111 <!-- Last page. -->
112 {{ if ne $paginator.PageNumber $paginator.TotalPages }}
113 <li>
114 <a class="page" href="{{ $paginator.Last.URL }}">
115 <span class="button small">LAST</span>
116 </a>
117 </li>
118 {{ end }}
119
120 </ul><!-- .pagination -->
121 {{ end }}