HUGO

Correct method to list section or type content?

Hi everyone.

I’m creating a portfolio website with a ‘projects’ section/type. Which method is considered the correct best-practice to display content of this section/type content on the homepage?

Each of the following work (code used within index.html), and forum searching or Hugo’s documentation do not make it clear to me:

One

{{ range where .Pages "section" "projects" }}
	{{ .Render "summary" }}
{{ end }}

Two

{{ range .Pages }}
	{{ if eq .Type "projects" }}
		{{ .Render "summary" }}
	{{ end }}
{{ end }}

Three

{{ with .GetPage "projects" }}
	{{ range .Pages }}
		{{ .Render "summary" }}
	{{ end }}
{{ end}}

I’m trying to understand the use-case for each.

Thanks!

I would use the last, this is the fastest in generation

1 Like

Thank you @ju52 for your speedy reply. I will amend my code.

Out of interest, my projects page (accessed directly) uses the list.html template. It includes the simple ‘if… else…’ below. Could this also be written more efficiently? i.e. checking which list page we are on?

{{ define "main" }}
	
	<h1>{{ .Title }}</h1>

	{{ .Content }}
	
	{{ if eq .Section "projects" }}
		<div class="projects">
			{{ range .Pages }}
				{{- .Render "summary" -}}
			{{ end }}
		</div>
	{{ else }}
		<ul class="posts">
			{{ range .Pages }}
				{{- .Render "li" -}}
			{{ end }}
		</ul>
	{{ end }}

{{ end }}

copy the list.html to layouts/projects and layouts/posts

You can modify it for the special section

Please read the manuals :wink:

1 Like

Thank you again, @ju52. I have indeed read the manual for this particular issue, and am aware of the correct lookup order… I just prefer to use the minimum files necessary.

Seems unnecessarily redundant to have an extra folder, and another file that would be virtually identical, except for the desired “.Render” view, that’s all.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.