Is it possible to group resources by directory?


Let’s say I have several subdirs in a resource bundle. Is it possible to cycle through them in a clean way? I want to get all content from subdir1, process it. Then all from subdir2, process it. And so on, for an arbitrary number of subdirs.

I could use ReadDir but I don’t like that, AFAIK, I have to pass the full path from the Hugo root - and I don’t see a way to get the path to the current page.

I could also try to play with permalinks to see if they share a common prefix, but this doesn’t look straightforward.

Many thanks for any clean solution :slight_smile:

I bit the bullet and ended up splitting and stitching together .Name to get all subdirs with precisely one markdown file, which is my use case, but I guess that a general form is just a uniq ( sort $dirs ) ) away. I’ll then use .Resources.Match from the caller to get the resources in each subdir.

{{ $dirs := slice }}
{{ with .Match "*/*.md" }}
  {{ range . }}
    {{ $path_parts := split .Name "/" }}
    {{ $parts_count := sub ( len $path_parts ) 1}}
    {{ $dir := ( delimit (first $parts_count $path_parts) "/" ) }}
    {{ $dirs = $dirs | append $dir }}
  {{ end }}
{{ end }}
{{ return $dirs }}

I still consider it too complex, but I can live with it after confining it in its own partial.

a page variable has this attributes .Dir, .File with .File.Path and .File.Path to access the current path.
Use the path- funtions to work with the path “strings”, should work on all supported OSes

Yep, path.Dir definitely cut down the complexity :joy: thanks! And good to know about the file variables, I missed that section in the docs and I precisely needed .UniqueID for other stuff.

I’ve done it with

for my ATOM feeds like this

{{ $uuid := sha1 (.Permalink | absURL)}}
<id>urn:uuid:{{substr $uuid 0 8}}-{{substr $uuid 8 4}}-5{{substr $uuid 13 3}}-{{substr $uuid 16 1}}9{{substr $uuid 17 2}}-{{substr $uuid 21 12}}</id>

urn:uuid must follow some rules

Interesting, I didn’t know it was so easy to craft a valid uuidv5, I’ll keep that in mind

there is a uuid Go-module, I suggested to include …