【问题标题】:runtime error: invalid memory address or nil pointer dereference on pagination运行时错误:无效的内存地址或分页上的 nil 指针取消引用
【发布时间】:2020-12-25 14:44:48
【问题描述】:

我正在尝试重新编写分页部分,但编译时出现错误

目前我正在处理 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>

但我得到了错误

错误 2020/09/07 03:31:55 无法渲染页面:渲染“部分”失败:执行模板失败:模板:部分/post.html:22:5:在 :错误调用部分:“../layouts/partials/pagination.html:75:56”:执行模板失败:模板:部分/分页.html:75:56:执行“部分/分页。 处的 html":错误调用 URL:运行时错误:无效的内存地址或 nil 指针取消引用

完整的sn-p

{{ $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 }}

【问题讨论】:

    标签: pagination hugo


    【解决方案1】:

    看着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)
    }
    

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

    如果没有页面,按钮将被禁用而不是隐藏

    所以p 很可能是nil

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

    如果有更安全的方法来进行相同的测试,请查看paginator_test.go

    OP 在 cmets 中指出:

    我的问题是不了解编译器的工作原理。
    类似的东西

    <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> 
    

    不起作用,因为在 $paginator.HasNextfalse(最后一页)的情况下调用了 $paginator.Next.URL

    我最终使用了{{else}} 声明

    【讨论】:

    • 我认为我的问题是不了解编译器是如何工作的。 &lt;li class="page-item {{ if $paginator.HasNext }}{{else}}disabled{{end}}"&gt;&lt;a class="page-link" href="{{ $paginator.Next.URL }}"&gt;&lt;i class="fal fa-chevron-right fa-xs"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt; 之类的东西不起作用,因为在 $paginator.HasNext 为假(最后一页)的情况下正在调用 $paginator.Next.URL。我最终使用了{{else}} 声明。
    • @lizzy91 好点。我已将您的评论包含在答案中以提高知名度。
    【解决方案2】:

    最终解决方案:

            <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

    【讨论】:

      猜你喜欢
      • 2014-12-30
      • 2015-07-12
      • 2012-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-24
      • 2013-09-16
      • 2022-01-05
      相关资源
      最近更新 更多