How to render contents of section _index.md and child pages

I have similar problem. I’ve created content/why with _index.md and some files like block1.md, block2.md, etc.

I have in theme layouts/_default/why.html:

{{define "main"}}
<div class="col col-4">
<img src="{{.Params.icon}}" alt="">
{{.Content}}
</div>
{{end}}

and i don’t know how to render that in my partial template? For now i did this:

<div class="col col-3">
				{{ range last 1 (where .Site.Pages "Section" "why") }}
					<h2>{{.Title}}</h2>
					{{.Content}}
				{{end}}
			</div>
			<div class="col col-8">
				<div class="row">
					{{ range first 6 (where .Site.Pages "Section" "why") }}
					<div class="col col-4">
						<img src="{{.Params.icon}}" alt="">
						{{.Content}}
					</div>
					{{end}}
				</div>
			</div>

and this works - in col-3 i get _index.md and in col-8>.row>.col-4 rest of files.
BUT i wish to render _index.md from this section and then rest from section template in _default/why.html

How can i do that (and is there something to render just _index.md the clean way)?

Thanks for any help.

you all must dive deeper :wink:

for hugo new it’s a good way to create archetypes.

Every section renders with _default/single.html, IF there is not a layouts/<some_section>/single.html template. Same with list.html.

Search in the doc for template lookup order.

I don’t want to and don’t need to create archetype for this section as this is something only on homepage. I wanted to do it proper way with option to add more content easily using section template, but i don’t understand this part as hugo don’t see layout for this section - archetypes will make this to happen?

you cat “preset” something with the archetypes

my sample:

+++
categories  = ["Photo"]
tags        = ["{{ now.Format "2006"}}", "Panorama"]
description = "{{ replace .TranslationBaseName "-" " " | title }}"
title       = "{{ replace .TranslationBaseName "-" " " | title }}"
date        = "{{ .Date }}"
draft       = false
layout      = "panorama"
series      = []
+++
## H2

Hi @maciejc,

I’ve moved your post to its own topic as the one you were replying to was several years old and there have been a lot of changes to Hugo since then.

As for your question, if I understood it correctly, you want to render the contents of a section page (content/why/_index.md) and then render the contents of its child pages separately. Try something like this:

{{ $why := .Site.GetPage "/why" }} <!-- get section page content/why/_index.md -->
{{ $why.Content }}
<br>
{{ range $why.Pages }} <!-- get children pages: content/why/block.md -->
  {{.Content}}
  <br>
{{ end }}
1 Like

Thank you, this is much more elegant solution. So in cases like this there’s no need for custom list layout and it’s ok (or is it good hugo practice in this case)?

It really depends on what you want to do. There are different ways to achieve the same results in Hugo.