我正在尝试列出当前 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/
最佳答案
您需要过滤.Site.Pages
按部分使用 where
function .尝试这个:
<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 }}
关于hugo - 如何列出当前部分中的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42868110/