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.
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 thanks! And good to know about the file variables, I missed that section in the docs and I precisely needed .UniqueID for other stuff.