List files in a directory inside a template

Hello,

I am trying to access file names located in a directory, and iterate through them in a template, to get a “dynamic” menu.

I tried using a Go function (readDir), but it doesn’t work.
Is there anything I’m missing?

Sorry if this isn’t really clear, it’s my first post, I’ll be glad to expand on the topic if someone’s willing to give me a hand.

Best regards,
Shaylee.

Edit:
I tried using the “browse” directive, but I get an error:

[ERROR 500 /src/file.md] template: :9:6: executing “” at <.Path>: can’t evaluate field Path in type markdown.Data

Hi Shaylee!

Not every Go function is exposed for templates to use. Unless you’re using the browse directive, I don’t think we have one right now that you can use which would give you a list of files. However, that’s only for browse templates which sounds like isn’t really what you want – you just want to use the templates directive and have a way to get a list of files.

I don’t see why this would be a problem to implement – it shouldn’t be a security concern since the site owner creates the templates (I guess you could shoot yourself in the foot but this isn’t the only way).

Would you please be able to open an issue on GitHub to request this feature? It should be fairly straightforward to implement, I’m sure a new contributor would love to hack on it. Maybe even yourself! :slight_smile:

Thanks for the answer. I posted a Github issue (https://github.com/mholt/caddy/issues/1198) as you suggested, and I will start digging in the sources to see if I can do it myself.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.