How can I include sections and pages inside a range loop?
For example, when I am at the top product section I want to display the summary text and title of section1(_index.html) , section2(_index.html) and product3(index.html)
list.html
{{ range $index, $element := .Pages }}
{{ .Render "summary"}}
{{ end }}
summary.html
{{- with .Page -}}
{{- .Name -}}
{{- .Summary -}}
_index.html
---
title: „Section Name“
date: 2020-05-26T18:00:00+01:00
draft: false
summary: „Summary Text“
---
product.html
---
title: „Product Title“
date: 2020-05-26T18:00:00+01:00
draft: false
summary: „Summary Text“
---
Page layout
content/
├── _index.html
├── products
│ ├── _index.html
│ ├── section1
│ │ ├── _index.html
│ │ ├── product11
│ │ ├── product12
│ └── section2
│ │ ├── _index.html
│ │ ├── product21
│ └── product3
│ │ ├── index.html
You have sub-sections under each section and you want to range through them.
This is possible through a nested loop.
{{ range .Site.Sections }}
{{ range .Sections }}
<h1>Section: {{ .Title }}</h1>
<p> Summary: {{ .Summary }}</p>
... implement your logic
{{ end }}
{{ end }}
That would only show me the sections and leave out the pages, in this case products. When I am at /products I want to list the title and summary of
section1/_index.html
section2/_index.html
product3/index.html
With range sections or range pages I can only get section*/_index or product*/index.html but not both.
You can always range on Pages inside a section.
Within a particular page or section, you can also refer to it’s parent using the .Parent
In your case, this would be similar to:
{{ range .Site.Sections }}
{{ range .Sections }}
<h1>Section: {{ .Title }}</h1>
{{ range .Pages }}
<h2>Page under {{ .Parent.Title }}: {{ .Title }}</h2>
<p> Summary: {{ .Summary }}</p>
... implement your logic
{{ end }}
{{ end }}
{{ end }}
Found a solution that works for me.
{{ $items := .Pages | append .Sections }}
{{ range $index, $item := (sort $items ".Params.weight" "asc") }}
... logic
{{ end }}
system
Closed
June 23, 2020, 4:27pm
6
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.