{{ $pag := $.Paginator }}

{{ if gt $pag.TotalPages 1 }}
    <ul class="pagination">
        {{ with $pag.First }}
        {{ end }}

        {{ $.Scratch.Set "__paginator.ellipsed" false }}
        {{ range $pag.Pagers }}
            {{ $right := sub .TotalPages .PageNumber }}
            {{ $showNumber := or (le .PageNumber 1) (eq $right 0) }}
            {{ $showNumber := or $showNumber (and (gt .PageNumber (sub $pag.PageNumber 3)) (lt .PageNumber (add $pag.PageNumber 3)))  }}
            {{ if $showNumber }}
                {{ $.Scratch.Set "__paginator.ellipsed" false }}
                {{ $.Scratch.Set "__paginator.shouldEllipse" false }}
            {{ else }}
                {{ $.Scratch.Set "__paginator.shouldEllipse" (not ($.Scratch.Get "__paginator.ellipsed") ) }}
                {{ $.Scratch.Set "__paginator.ellipsed" true }}
            {{ end }}
            {{ if $showNumber }}
                <li class="page-item {{ if eq . $pag }}active{{ end }}">
                    <span class="page-link">
                        <a href="{{ .URL }}">{{ .PageNumber }}</a>
                    </span>
                </li>
            {{ else if ($.Scratch.Get "__paginator.shouldEllipse") }}
                <li class="page-item "><span class="page-link" aria-hidden="true">&hellip;</span></li>
            {{ end }}
        {{ end }}
    </ul>
{{ end }}
