hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

pagination.html (5679B)

    1 {{- $validFormats := slice "default" "terse" }}
    2 
    3 {{- $msg1 := "When passing a map to the internal pagination template, one of the elements must be named 'page', and it must be set to the context of the current page." }}
    4 {{- $msg2 := "The 'format' specified in the map passed to the internal pagination template is invalid. Valid choices are: %s." }}
    5 
    6 {{- $page := . }}
    7 {{- $format := "default" }}
    8 
    9 {{- if reflect.IsMap . }}
   10   {{- with .page }}
   11     {{- $page = . }}
   12   {{- else }}
   13     {{- errorf $msg1 }}
   14   {{- end }}
   15   {{- with .format }}
   16     {{- $format = lower . }}
   17   {{- end }}
   18 {{- end }}
   19 
   20 {{- if in $validFormats $format }}
   21   {{- if gt $page.Paginator.TotalPages 1 }}
   22     <ul class="pagination pagination-{{ $format }}">
   23       {{- partial (printf "partials/inline/pagination/%s" $format) $page }}
   24     </ul>
   25   {{- end }}
   26 {{- else }}
   27   {{- errorf $msg2 (delimit $validFormats ", ") }}
   28 {{- end -}}
   29 
   30 {{/* Format: default
   31 {{/* --------------------------------------------------------------------- */}}
   32 {{- define "partials/inline/pagination/default" }}
   33   {{- with .Paginator }}
   34     {{- $currentPageNumber := .PageNumber }}
   35 
   36     {{- with .First }}
   37       {{- if ne $currentPageNumber .PageNumber }}
   38       <li class="page-item">
   39         <a href="{{ .URL }}" aria-label="First" class="page-link" role="button"><span aria-hidden="true">&laquo;&laquo;</span></a>
   40       </li>
   41       {{- else }}
   42       <li class="page-item disabled">
   43         <a aria-disabled="true" aria-label="First" class="page-link" role="button" tabindex="-1"><span aria-hidden="true">&laquo;&laquo;</span></a>
   44       </li>
   45       {{- end }}
   46     {{- end }}
   47 
   48     {{- with .Prev }}
   49       <li class="page-item">
   50         <a href="{{ .URL }}" aria-label="Previous" class="page-link" role="button"><span aria-hidden="true">&laquo;</span></a>
   51       </li>
   52     {{- else }}
   53       <li class="page-item disabled">
   54         <a aria-disabled="true" aria-label="Previous" class="page-link" role="button" tabindex="-1"><span aria-hidden="true">&laquo;</span></a>
   55       </li>
   56     {{- end }}
   57 
   58     {{- $slots := 5 }}
   59     {{- $start := math.Max 1 (sub .PageNumber (math.Floor (div $slots 2))) }}
   60     {{- $end := math.Min .TotalPages (sub (add $start $slots) 1) }}
   61     {{- if lt (add (sub $end $start) 1) $slots }}
   62       {{- $start = math.Max 1 (add (sub $end $slots) 1) }}
   63     {{- end }}
   64 
   65     {{- range $k := seq $start $end }}
   66       {{- if eq $.Paginator.PageNumber $k }}
   67       <li class="page-item active">
   68         <a aria-current="page" aria-label="Page {{ $k }}" class="page-link" role="button">{{ $k }}</a>
   69       </li>
   70       {{- else }}
   71       <li class="page-item">
   72         <a href="{{ (index $.Paginator.Pagers (sub $k 1)).URL }}" aria-label="Page {{ $k }}" class="page-link" role="button">{{ $k }}</a>
   73       </li>
   74       {{- end }}
   75     {{- end }}
   76 
   77     {{- with .Next }}
   78       <li class="page-item">
   79         <a href="{{ .URL }}" aria-label="Next" class="page-link" role="button"><span aria-hidden="true">&raquo;</span></a>
   80       </li>
   81     {{- else }}
   82       <li class="page-item disabled">
   83         <a aria-disabled="true" aria-label="Next" class="page-link" role="button" tabindex="-1"><span aria-hidden="true">&raquo;</span></a>
   84       </li>
   85     {{- end }}
   86 
   87     {{- with .Last }}
   88       {{- if ne $currentPageNumber .PageNumber }}
   89       <li class="page-item">
   90         <a href="{{ .URL }}" aria-label="Last" class="page-link" role="button"><span aria-hidden="true">&raquo;&raquo;</span></a>
   91       </li>
   92       {{- else }}
   93       <li class="page-item disabled">
   94         <a aria-disabled="true" aria-label="Last" class="page-link" role="button" tabindex="-1"><span aria-hidden="true">&raquo;&raquo;</span></a>
   95       </li>
   96       {{- end }}
   97     {{- end }}
   98   {{- end }}
   99 {{- end -}}
  100 
  101 {{/* Format: terse
  102 {{/* --------------------------------------------------------------------- */}}
  103 {{- define "partials/inline/pagination/terse" }}
  104   {{- with .Paginator }}
  105     {{- $currentPageNumber := .PageNumber }}
  106 
  107     {{- with .First }}
  108       {{- if ne $currentPageNumber .PageNumber }}
  109       <li class="page-item">
  110         <a href="{{ .URL }}" aria-label="First" class="page-link" role="button"><span aria-hidden="true">&laquo;&laquo;</span></a>
  111       </li>
  112       {{- end }}
  113     {{- end }}
  114 
  115     {{- with .Prev }}
  116       <li class="page-item">
  117         <a href="{{ .URL }}" aria-label="Previous" class="page-link" role="button"><span aria-hidden="true">&laquo;</span></a>
  118       </li>
  119     {{- end }}
  120 
  121     {{- $slots := 3 }}
  122     {{- $start := math.Max 1 (sub .PageNumber (math.Floor (div $slots 2))) }}
  123     {{- $end := math.Min .TotalPages (sub (add $start $slots) 1) }}
  124     {{- if lt (add (sub $end $start) 1) $slots }}
  125       {{- $start = math.Max 1 (add (sub $end $slots) 1) }}
  126     {{- end }}
  127 
  128     {{- range $k := seq $start $end }}
  129       {{- if eq $.Paginator.PageNumber $k }}
  130       <li class="page-item active">
  131         <a aria-current="page" aria-label="Page {{ $k }}" class="page-link" role="button">{{ $k }}</a>
  132       </li>
  133       {{- else }}
  134       <li class="page-item">
  135         <a href="{{ (index $.Paginator.Pagers (sub $k 1)).URL }}" aria-label="Page {{ $k }}" class="page-link" role="button">{{ $k }}</a>
  136       </li>
  137       {{- end }}
  138     {{- end }}
  139 
  140     {{- with .Next }}
  141       <li class="page-item">
  142         <a href="{{ .URL }}" aria-label="Next" class="page-link" role="button"><span aria-hidden="true">&raquo;</span></a>
  143       </li>
  144     {{- end }}
  145 
  146     {{- with .Last }}
  147       {{- if ne $currentPageNumber .PageNumber }}
  148       <li class="page-item">
  149         <a href="{{ .URL }}" aria-label="Last" class="page-link" role="button"><span aria-hidden="true">&raquo;&raquo;</span></a>
  150       </li>
  151       {{- end }}
  152     {{- end }}
  153   {{- end }}
  154 {{- end -}}