Access data from other files with range

Hi Folks,

I am using Forestry with Repeater element to reference the related articles of the current page. But I’m having trouble in retrieving the data of the referenced file.

Here’s what I had in work-1.md

related_works:
- works_item: works/work-2.md
- works_item: works/work-3.md

and here’s what I had in single.html

{{ $related_works := .Params.related_works }}
{{ range $related_works }}
    <h2>{{ .Title}}</h2>
{{ end }}

How do I retrieve the title of work-2.md and work-3.md?

My expected behavior is something like:

<h1>Working Title 1</h1>
<h3>Other related phones</h3>
<h2>Working Title 2</h2>
<h2>Working Title 3</h2>

I am not quite sure how it’s going to work, would be a great help if anyone knows how, thank you!

Yes, you can use .GetPage function. (Lot of example in the documentation)

with structure like this:

related_works:
- works_item: works/work-2.md
- works_item: works/work-3.md

I’m not tested it yet, but something like this. :face_with_monocle:

{{ $related_works := .Params.related_works }}
<h3>Other related phones</h3>
{{ range $path := $related_works }}
  {{ with $.Site.GetPage $path.works_item }}
   <h2> {{ .Title }}</h2>
  {{ end }}
{{ end }}
1 Like

Thanks for prompt reply!

I’ve tried your solution but receiving this error:
execute of template failed: template: works/single.html:97:25: executing "main" at <$path>: wrong type for value; expected string; got map[string]interface {}

Yep, sorry, fixed, please test it again :open_mouth:

It works!!! Yay, thank you very much! :heart_eyes:

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