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}}
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 }}
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.