Just to add a little bit of clarification… .Resources.Match is syntactically correct too, but not when used in this context: ( .Resources.Match "code.txt" ).Content – Docs on .Resources methods.
.Resources.Match → Returns a slice of matches… so you need to then use a range to go through the returned slice/list.
.Resources.GetMatch → Returns only the first match.
So yes, .Resources.GetMatch is the right method in this context.
I’ve set layout=pagein my frontmatter, so it should be fine to use that template. And it works fine when I use the readFile method. Maybe I should change the structure, but I don’t think that is what cause the error…
I’ve setup a test repo, with code that works with readFile. But I cannot get it to work with Resources.Content.
Click on My Product on the home page to test it.
I also cannot get the layout/page/single.html structure to work, nor the layout/single.html structure - that’s why its now structured as layouts/_default/page.html and layout: page is set in the front matter.
Use .Resource.Content instead of {{- $file := .Params.codefile }} {{ readFile $file }} in the code.html partial. The readFile method works fine, but requires me to define the path of the file in the front matter.
I had a look at your test repository. The error you experienced was due to the fact that Hugo could not find the resource from within the partial you defined. To achieve what you want change your code.html partial to the following
Note the addition of .Page.Parent This tells Hugo to look for a resource from within another resource that belong in the same Page Bundle.
Why do you need to do that? Because of the way you have structured your page.html template:
{{ define "main" }}
{{- with .Resources.Match "codeexample.md" }}
{{- range . }}
{{ .Render "codeexample" }}
{{- end }}
{{- end }}
{{ end }}
In your template you are calling the codeexample.md resource and then from within its range you are rendering your codeexample.html template. Hence the context is lost when you’re trying to call the .Content of the resource code.txt from within your code.html partial.
Thank you so much for your detailed reply - it makes perfect sense the way you present it. I haven’t had that much time with Hugo yet, so I don’t know all the intricacies