2017-03-17 22 views
1

我試圖列出當前網址部分中的頁面。如何列出當前部分中的頁面

  • 獲取所有部分
  • 限制範圍內當前部分
  • 列表頁,當前第

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

回答

2

您需要使用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 }} 
+0

你認爲你將能夠幫助:https://stackoverflow.com/questions/46848467/hugo-error-calling-where-cant-evaluate-the-array-by-no-match -argument或更多的 – JamesG

相關問題