Add link to title from scratch output

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>

Did you already try the snippet that @pointyfar gave in another thread?

Yes but I need this to be dynamic - it has to work for multiple sections and not just “projects”

I see. Try tweaking the snippet like so:

{{ range first 12 (sort .Sections "Date" "desc") }} 
  <a href="{{ .RelPermalink }}">{{ .Title }}</a>
{{ end }}

My folder structure is like this:
content
— project 1
------- folder 1
---------- content 1
---------- content 2
---------- content 3

------- folder 2
---------- content 4
---------- content 5

------- folder 3
---------- content 6
---------- content 7

— project 2
— project 3

“project 1” list page should show content 1 - 7 (based on date order)

My folder structure is like this:
content
–projects
— project 1
------- folder 1
---------- content 1
---------- content 2
---------- content 3

------- folder 2
---------- content 4
---------- content 5

------- folder 3
---------- content 6
---------- content 7

— project 2
— project 3

“projects” page should show all posts in “project 1” in a single list. Needs to be dynamic so I can apply this to “project 2” and “project 3”

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.

1 Like

This is the code I currently have:

        <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

Hi,

A few things:

  1. It’s difficult to keep track of what the actual question is when you keep opening new topics to solve essentially the same issue.
  2. What is it you actually want to do? What is the ‘input’ content structure and what is the target ‘output’ html list?
  3. 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/

Hi,

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)

Were you able to figure this out? I have a similar use case.

Hi there,

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.