runtime error: invalid memory address or nil pointer dereference on pagination

runtime error: invalid memory address or nil pointer dereference on pagination

我正在尝试重写我的部分分页,但我在编译时遇到错误

目前我正在处理 nextprev

        {{ if $paginator.HasNext }}
            <li class="page-item">
                <a class="page-link" href="{{ $paginator.Next.URL }}">
                    <i class="fal fa-chevron-right fa-xs"></i>
                </a>
            </li>
        {{ end }}

我正在尝试修改它,以便如果没有页面,按钮将被禁用而不是隐藏

        <li class="page-item {{ if eq $paginator.HasNext false }}disabled{{ end }}">
            <a class="page-link" href="{{ $paginator.Next.URL }}">
                <i class="fal fa-chevron-right fa-xs"></i>
            </a>
        </li>

但我收到错误

ERROR 2020/09/07 03:31:55 Failed to render pages: render of "section" failed: execute of template failed: template: section/post.html:22:5: executing "main" at <partial "pagination" .>: error calling partial: "../layouts/partials/pagination.html:75:56": execute of template failed: template: partials/pagination.html:75:56: executing "partials/pagination.html" at <$paginator.Next.URL>: error calling URL: runtime error: invalid memory address or nil pointer dereference

完整片段

{{ $paginator := .Paginator }}

<!-- Number of links either side of the current page. -->
{{ $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 }}
    <nav aria-label="Page navigation example">
        <ul class="pagination justify-content-center">

            {{/* First */}}
            <li class="page-item {{ if ne $paginator.PageNumber 1 }}disabled{{ end }}">
                <a class="page-link" href="{{ $paginator.First.URL }}">
                    <i class="fal fa-chevron-double-left fa-xs"></i>
                </a>
            </li>

            {{/* Prev */}}
            {{ if $paginator.HasPrev }}
                <li class="page-item">
                    <a class="page-link" href="{{ $paginator.Prev.URL }}">
                        <i class="fal fa-chevron-left fa-xs"></i>
                    </a>
                </li>
            {{- end -}}

            {{/* Page # */}}
            {{ range $paginator.Pagers }}
                {{ $.Scratch.Set "page_number_flag" false }}
                {{ if gt $paginator.TotalPages $max_links }}
                    {{ if le $paginator.PageNumber $lower_limit }}
                        {{ if le .PageNumber $max_links }}
                            {{ $.Scratch.Set "page_number_flag" true }}
                        {{ end }}
                    {{ else if ge $paginator.PageNumber $upper_limit }}

                        {{ if gt .PageNumber (sub $paginator.TotalPages $max_links) }}
                            {{ $.Scratch.Set "page_number_flag" true }}
                        {{ end }}
                    {{ 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 }}
                {{ else }}
                    {{ $.Scratch.Set "page_number_flag" true }}
                {{ end }}
                {{ if eq ($.Scratch.Get "page_number_flag") true }}
                    {{ $isCurrent := false }}
                    {{ if eq . $paginator }}
                        {{ $isCurrent = true }}
                    {{ end }}
                    <li class="page-item {{ if $isCurrent }}active{{ end }}"
                        {{ if $isCurrent }}aria-current="page"{{ end }}>
                        <a class="page-link" href="{{ .URL }}">
                            {{ .PageNumber }}{{ if $isCurrent }}<span class="sr-only">(current)</span>{{ end }}
                        </a>
                    </li>
                {{ end }}
            {{ end }}

            {{/* Next */}}
            {{ if $paginator.HasNext }}
                <li class="page-item">
                    <a class="page-link" href="{{ $paginator.Next.URL }}">
                        <i class="fal fa-chevron-right fa-xs"></i>
                    </a>
                </li>
            {{ end }}


{{/*            Error here*/}}
                <li class="page-item {{ if eq $paginator.HasNext false }}disabled{{ end }}">
                    {{ if $paginator.HasNext }}
                        <a class="page-link" href="{{ $paginator.Next.URL }}">
                            <i class="fal fa-chevron-right fa-xs"></i>
                        </a>
                    {{ end }}
                </li>




            {{/* Last */}}
            <li class="page-item {{ if eq $paginator.PageNumber $paginator.TotalPages }}disabled{{ end }}">
                <a class="page-link" href="{{ $paginator.Last.URL }}">
                    <i class="fal fa-chevron-double-right fa-xs"></i>
                </a>
            </li>
        </ul>
    </nav>
{{ end }}

查看 resources/page/pagination.go,我看到:

// HasNext tests whether there are page(s) after the current.
func (p *Pager) HasNext() bool {
    return p.PageNumber() < len(p.paginatedElements)
}

问题是你想要(强调我的):

if theres no pages, the button is disabled rather than hidden

所以p很可能是nil

你的{{ if gt $paginator.TotalPages 1 }}应该是指:没有页面left.
但是,为了测试,请尝试使用 if eq $paginator.PageNumber $paginator.TotalPages 进行测试,并在这种情况下禁用按钮。

检查 paginator_test.go 是否有更安全的方法来进行相同的测试。

OP 在评论中指出:

my issue was not understanding how the compiler worked.
Something like

<li class="page-item {{ if $paginator.HasNext }}{{else}}disabled{{end}}">
  <a class="page-link" href="{{ $paginator.Next.URL }}">
  <i class="fal fa-chevron-right fa-xs"></i></a>
</li> 

would not work because $paginator.Next.URL is being called in scenarios where $paginator.HasNext is false (last page).

I ended up using an {{else}} statement

最终解决方案:

        <li class="page-item {{ if not $paginator.HasNext }}disabled{{- end -}}">
            <a class="page-link"
               href="{{ if $paginator.HasNext }}{{ $paginator.Next.URL }}{{- else -}}#{{- end -}}">
                <i class="fal fa-chevron-right fa-xs"></i>
            </a>
        </li>

我的问题是在调用 {{ $paginator.Next.URL }} 之前未进行安全检查,这导致 nil 何时呈现最后一页。