HUGO

How do I iterate over Sections, and get data from one of the sub-items

I’m trying to output a Section/collection item’s data onto a page but I can’t find the correct way to do this.

I can list each section like this but then I can’t figure out how to select a section and work with the data within that section item:

{{ range .Site.Sections }}
  <li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
{{ end }}

In Jekyll this is what I would be doing:

Front Matter
---
title: Post 1
authors:
  - 'betty-white'
---
Liquid
{% for author in page.authors %}
	{% assign person_relative_url = author | prepend: "_people/" | append: ".html" %}
	{% assign person = site.people | where: 'relative_path', person_relative_url | first %}
	<p>{{ person.name }}</p>
	<img src="{{ person.image }}" alt="{{ person.name }}" />
{% endfor %}

What would be the equivalent with Hugo?

In Hugo, a Section is also a Page. So,

{{ range .Site.Sections }}
  {{ $foo := . }} <!-- Page corresponding to current iteration, i.e. a Section -->
  {{ $foo.Pages }} <!-- Children pages belonging to $foo, which you can also range over  -->
{{ end }}

Great, thank you! Here’s what I’ve got so far:

{{ range $index, $collections := .Site.Sections }}
	{{ if eq $collections.Title "People" }}
		{{ $collection := . }}
		<ul>
			{{ range $collection.Pages }}
	  			{{ $item := . }}
	  			{{ if $item.Params.name }}
		  			<li>{{ $item.Params.name }}</li>
  				{{ end }}
			{{ end }}
		</ul>
	{{ end }}
{{ end }}

Is there a way of specifying the actual Section/collection rather than iterating over all of them and checking the title like in my example?

I’d suggest where (again). Something like

{{ range .Site.Sections where "Title" "People" }}
<ul>
  {{ range .Pages }} 
     {{ if .Params.name }}

This is actually taken nearly verbatim from the first example in the documentation for where.

I think that you do not need all these variables ($collections, $item), since range just sets the dot which you can then use. Also, you do not use $index, so no need to define it.

Thank you @chrillek, I’ve shortened everything to the example below but now I get an error.

{{ range .Site.Sections where "Title" "People" }}
	<ul>
		{{ range .Pages }}
	  		<li>{{ .Params.name }}</li>
		{{ end }}
	</ul>
{{ end }}

Error:

execute of template failed: template: posts/single.html:7:19: executing "main" at <.Site.Sections>: wrong number of args for Sections: want 0 got 3

If I output all Section titles I get Posts and People so I’m not sure why I’m getting this error

{{ $mysection := site.GetPage "/my-section/" }}
{{ range $mysection.Pages }}
...
{{ end }}

Doc: .GetPage | Hugo

Thanks @pointyfar! Here’s my final code snippet:

{{ $collection := site.GetPage "/people/" }}
{{ $authors := .Params.authors }}
{{ range $collection.Pages }}
	{{ $name_slug := .Params.name | urlize}}
	{{ if in $authors $name_slug }}
		<li>{{ .Params.name }}</li>
	{{ end }}
{{ end }}

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