[SOLVED] Check if .File is Directory?

Hey there,

First, I cannot use Page Resource on this project :frowning:

I need to output a tree structure of files with download link (grouped by directory)
Here is my dir structure from static or else:

└── docs
    β”œβ”€β”€ dir-1
    β”‚   β”œβ”€β”€ a-file.pdf
    β”‚   └── another-file.xlm
    β”œβ”€β”€ dir-2
    β”‚   β”œβ”€β”€ a-file.pdf
    β”‚   └── another-file.xlm
    └── anoying_file.pdf

The template would look like this:

Something like this:

{{ $docs := readDir "content/docs" }}
{{ range $docs }}
	{{ if .IsDirectory }}
    	{{ range . }}
    		{{ printf "%#v" . }}
    	{{ end }}
	{{ end }}
{{ end }}

.IsDirectory doesn’t exist, I’m looking for way to do just that though.
How can I check if the β€œitem” is a file or a directory and operate range on it or not.

Help would be much appreciated!

Try:

IsDir.

https://golang.org/pkg/os/#FileInfo

2 Likes

Thanks will try that!

not (strings.Contains .Name ".") didn’t feel right…

Thanks .IsDir works like a charm!
It’s not always easy to know which Go method/variable is available in Go Template. But the file object seemed straight out of Go.
I’ll be more investigative next time!

https://godoc.org/github.com/gohugoio/hugo/tpl/os

Thanks! I’ll know where to look from now on. (tpl)