I have managed to output a list of posts using a scratch method but I now need to add the permalink to each title and sort by date. I’ve tried adding to the .Scratch.Add but without any luck.
My final output in the <p> tags should be something like:
<a href="{{.RelPermalink}}">{{.Title}}</a>
My current code:
<ul>
{{ range .Sections }}
<li><a href="{{ .RelPermalink }}">{{ .Title }}</a>
{{ $.Scratch.Set "test" (slice "") }}
{{ range .Sections }}
{{ range .Pages }}
{{ $.Scratch.Add "test" .Title }}
{{ end }}
{{ end }}
{{ $test := $.Scratch.Get "test"}}
{{ range first 12 $test }}
<p>{{ . }}</p>
{{ end }}
</li>
{{ end }}
</ul>
You shared a rough mockup of your file structure, but folks would be more likely to help you if you shared your code (the devil is in the details here).
If you can’t share the git repo, then create a small sample project that would be equivalent to the file structure you posted. For details on the best way to go about this, please see requesting help.
<ul>
{{ range .Sections }}
<li><a href="{{ .RelPermalink }}">{{ .Title }}</a>
{{ range .Pages }}
<ul>
<li><a href="{{ .RelPermalink}}">{{ .Title }}</a></li>
</ul>
{{ end }}
</li>
{{ end }}
</ul>
But it returns blank as there are no pages under “project 1”. I need to somehow pull them from all subfolders or “project 1” - if I add a post under “project 1” it outputs it. So I know I need to somehow loop through the subfolders and pull the pages. I don’t want to loop through each one and retrieve however as it will limit how I can sort the pages - such as posts
It’s difficult to keep track of what the actual question is when you keep opening new topics to solve essentially the same issue.
What is it you actually want to do? What is the ‘input’ content structure and what is the target ‘output’ html list?
As @zwbetz said above, it is easier to help you with a sample repo. We don’t need your full project, just enough of a structure that mirrors what you have so that when we talk about a snippet of code, when we run it we all get the same output.
Edited to add:
As for using Scratch ,
{{ range .Pages }}
{{ $.Scratch.Add "test" .Title }}
{{ end }}
the above creates an array of Page Titles, not an array of Page s which would have all the page variables
(eg .Permalink) accessible.
You should be able to achieve what (I think) you want by using range and where and the other things described here: https://gohugo.io/templates/lists/
I’m unable to make a repo at this time, I can try to do this in the near future.
The “projects” page (which sits immediately below content) should loop through and show “project 1”, “project 2”, “project 3”. I can get this working fine by using {{ range .Sections }}. The issue is however, these “project” folders will not always have their own content - only more folders which will contain the content.
So on “projects” I need to show ALL content below “project 1”, “project 2”, “project 3” regardless of if this content is in a sub folder.
Projects page will output something like:
Project 1
---- some content (from a sub folder)
---- some content (from a sub folder)
---- some content (from a sub folder)
Project 2
---- some content (from a sub folder)
---- some content (NOT from a sub folder)
---- some content (from a sub folder)
Project 3
---- some content (NOT from a sub folder)
---- some content (from a sub folder)
---- some content (from a sub folder)
I’m not sure what your actual question is, but I’m closing this year-old thread. Please open a new topic describing your issue. A sample repo would also be ideal.