【问题标题】:How to List Pages in Current Section如何列出当前部分中的页面
【发布时间】:2018-04-22 09:41:00
【问题描述】:

我正在尝试列出当前 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/

【问题讨论】:

    标签: hugo


    【解决方案1】:

    您需要使用where function 按部分过滤.Site.Pages。试试这个:

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

    【讨论】:

    猜你喜欢
    • 2021-05-05
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多