5

我正在尝试列出当前 url 部分中的页面。

  • 获取所有部分
  • 将范围限制为当前部分
  • 列出当前部分中的页面

{{ range $value := .Site.Sections }}

    {{ range .Section }}

        {{ range $value.Pages }}
            <ul>
                <li>{{ .Title }}</li>
            </ul>
        {{ end }}

    {{ end }}

{{ end }}

虽然它返回 null 因为{{ range .Section }}不是有效代码。

这样做的正确方法是什么?

https://gohugo.io/templates/variables/

4

1 回答 1

9

您需要.Site.Pages使用该where功能按部分过滤。尝试这个:

<ul>
{{ range where .Site.Pages "Section" .Section }}
  <li>{{ .Title }}</li>
{{ end }}
</ul>

如果要避免空列表,可以将部分页面的切片存储在变量中,并在输出 ul 标签之前检查其长度。

{{ $sectionPages := where .Site.Pages "Section" .Section }}
{{ if ge (len $sectionPages) 1 }}
  <ul>
    {{ range $sectionPages }}
      <li>{{ .Title }}</li>
    {{ end }}
  </ul>
{{ end }}
于 2017-04-17T06:15:40.020 回答