Hugo

Not able to access files from unknown folder

When I am looping through a know folder using .Site.Data.folderName, I am not able to access the inner folder name and files in the inner folder. Can anyone explain me the right way to do this.

apis (Main folder)
– api1 (Sub folder)
---- v1.yaml (File)
---- v2.yaml (File)

– api2 (Sub folder)
---- v1.yaml (File)
---- v2.yaml (File)

If I look at the doc example you should get access to the /data/apis/api1/v1.yaml file like so: .Site.Data.apis.api1.v1. But that’s not what happens for you it seems.

What code did you try to access those data files?

In my case I will be knowing only the parent directory path and name. The name/path of sub-directory will be unknown and needs to be fetched from current element in the range loop. Then I want to loop through the files available in sub-directory and get the name and content of the files. Could you please help to write this code? Currently, I am using below code which is not able to get the current file name in the range.

{{ range .Site.Data.apis }}
{{ range . }}
{{ range . }}
{{ .File.BaseFileName }}
{{ end }}
{{ end }}
{{ end }}

Here’s how I interpret your code:

<!-- Range over files in 'apis' folder -->
{{ range .Site.Data.apis }}

    <!-- Range over 'api1' and 'api2' subfolder -->
    {{ range . }}
    
        <!-- Range over 'v1' and 'v2' in those subfolders -->
        {{ range . }}
        
            <!-- Here the dot (.) refers to the files found
                in 'v1' and 'v2' -->
        
            <!-- But what is the goal here? -->
            {{ .File.BaseFileName }}
        {{ end }}
        
    {{ end }}
    
{{ end }}

I’m not sure what the goal of .File.BaseFileName is. If you want to access the data from inside the /data/ folder you don’t need .File.BaseFileName.

I was trying to get the current file name using {{ .File.BaseFileName }} i.e. v1.yaml, v2.yaml, etc. Could you please suggest the correct way to achieve this?

I don’t know more about your use case than I already shared. Perhaps someone else has a better insight. Else you might want to look at requesting help for what information people can use to help you.

1 Like