Display information from nested files

Hi!

I am new to Hugo so forgive me if this has already been asked.

I have the following structure:

content

  • about
    – our-team
    — _index.md
    — name1.md
    — name2.md
    — name3.md
    – sponsors
    — _index.md

On my homepage I have created a partial and would like to display each team member. However I can only seem to display the information from “about” and “sponsors” using the following:

{{ $sections := (.Site.GetPage "section" "about").Sections }}
{{ range $sections }}

    <p>Title: {{ .Title }}</p>

{{ end }}

I was hoping this would work but it breaks:

{{ $sections := (.Site.GetPage "section" "about/our-team").Sections }}
{{ range $sections }}

    <p>Title: {{ .Title }}</p>

{{ end }}

Any help would be appreciated. :pray:

To get pages from all Sections and Nested Sections I think that the above should be {{ $sections := (.Site.GetPage "section" .Section).Sections

Thanks for the help but sadly this didn’t do the trick. :frowning:

There must be a way to access markdown files in a subfolder. :thinking:

If I try the following:

{{ range $subsection := (.Site.GetPage "section" "about" "our-team") }}
   Section Page: {{ .Title }}<br>
{{ end }}

it returns the following error:

ERROR 2018/07/23 11:52:42 Error while rendering “home” in “”: template: index.html:42:11: executing “index.html” at <partial "meet-the-te…>: error calling partial: template: partials/meet-the-team.html:13:61: executing “partials/meet-the-team.html” at <“our-team”>: range can’t iterate over {0xc04419cd00 0xc042fb3ec0 section Pages(1) [] [] [0xc0440c4f00] map[date:{0 63624070800 0xc043428420} lastmod:{0 63624070800 0xc0434283c0} publishdate:{0 63624070800 0xc043428420} description:A list of team members title:Our team draft:false iscjklanguage:false]

Team members


Team members 0xc042dea840 [] [] [] false false {0 0 0 0} markdown true [45 45 45 13 10 100 97 116 101 58 32 50 48 49 55 45 48 51 45 48 50 84 49 50 58 48 48 58 48 48 45 48 53 58 48 48
13 10 116 105 116 108 101 58 32 34 79 117 114 32 116 101 97 109 34 13 10 100 101 115 99 114 105 112 116 105 111 110 58 32 34 65 32 108 105 115 116 32 111 102 32 116 101 97 109 32 109 101 109
98 101 114 115 34 13 10 45 45 45 13 10] [84 101 97 109 32 109 101 109 98 101 114 115] [60 112 62 84 101 97 109 32 109 101 109 98 101 114 115 60 47 112 62 10] false 0xc042cdb8b0 Team members
[] 0xc042062ac0 map[] _index { } [about our-team] 0xc042c03900 [] 0xc04241e300 Site(“DAFNI”) Our team A list of team members [] map[Pages:[0xc042c02a00]] {2017-03-02 12:00:00 -0500 -0500 2017-03-02 12:00:00 -0500 -0500 2017-03-02 12:00:00 -0500 -0500 0001-01-01 00:00:00 +0000 UTC} {monthly 0.5 sitemap.xml} {/about/our-team/ } http://localhost:1313/about/our-team/ /about/our-team/ about/our-team/ false {about about section en false} 0xc043aeb7a0 0xc04216a150 en [{HTML {text html . [html] } index canonical false true false false} {RSS {application rss xml . [xml] } index alternate false false true false}] 0xc043009180 0xc04302b4a0}
Total in 87 ms
ERROR 2018/07/23 11:52:42 Failed to rebuild site: logged 1 error(s)

Any thoughts as to what I have done wrong?

Please include a link to your Hugo project’s source code to investigate further.

Or if you cannot share include some dummy content.

Also regarding the error you get, if you are on the latest Hugo 0.45 that was released yesterday, please have a look at the release notes and associated documentation.

.GetPage has been improved.

I’ll try and include some dummy content.

I have updated the latest release and can use the following to display a title:

{{ with .Site.GetPage "/about/our-team" }}{{ .Title }}{{ end }}

Can this then be used with range to find all .md files with this folder?

Something like:

{{ range $subsection := (.Site.GetPage "/about/our-team") }}
    Section Page: {{ .Title }}<br>
{{ end }}

I am having problems myself with the latest release so I’m afraid that I cannot help further.

Maybe someone else will.

ok thank you for your help so far. :+1:

Would something like the following work? (I’m still on 0.44 though so not sure how the new GetPage does things yet)

{{ $subsection := .Site.GetPage "section" "about" "our-team" }}
  {{ range $subsection.Pages }}
    {{ .Title }}<br>
  {{ end }}
{{ end }}

The above will not work with Hugo 0.45

Absolutely smashed it! :point_up_2: :clap:

Are you on Hugo 0.45?

I rolled it back to 0.44

Ok. Good to know. Thanks!

On 0.45, above needs to be simply refactored to:

.Site.GetPage "section" "/about/our-team".


Update .Site.GetPage "/about/our-team" would be a more correct way (haven’t tested).

1 Like

That’s rarely a good solution :slight_smile: Always try to make your stuff work for future.

That is a fair point. I have updated to 0.45 and modified the code :sunglasses: