Hi,
I would like to know if it was possible to have a feature which would allow to list files in a directory, inside a template. The "browse" directive isn't really what I'm trying to achieve.
Here's a link to the forum thread: https://forum.caddyserver.com/t/list-files-in-a-directory-inside-a-template/841
Sorry if this isn't precise enough, if you have any questions, I'll be glad to answer them.
Am starting with go and would be interested to work on this.
@crackerplace Go for it. :)
I also started on this earlier in the week. I'll submit a PR, but feel free to reject it if @crackerplace has dibs. I don't want to step on any toes!
Since @tristanrollet cited Readdir() as an example, the new Files template action behaves similarly to it and Readdirnames, but doesn't take an integer as an argument.
I assumed that the directory would be within the root of the context. Let me know if that was incorrect.
@ericdreeves Sure , Please continue.I will work on some other issue.Not a problem.
@mholt Am looking into issues to find another issue to work with and which is fine for a newbie.
To @all working on this I just want to say: thank you. I am also very interested in improvements / changes regarding this.
I run a lot of mirrors for various projects (FOSS, etc,.) and I typically create a very simple directory index for the files, e.g something like this:
http://devuan.mirror.k0nsl.org/
But if this can be improved, I'd glad to implement those changes in my projects.
Was this ever actually resolved? When I try to use {{.Files "path"}} I get the following error:
30/Apr/2017:13:14:23 -0600 [ERROR 500 /] template: :13:14: executing "" at <.Include>: error calling Include: template: meta/tmpl/_index.html:7:2: executing "meta/tmpl/_index.html" at <.Files>: Files has arguments but cannot be invoked as function
I'm on Caddy 0.10.0 installed through Homebrew and Go 1.7.1.
Ah, so I think I know why I'm getting that error. There's already a .Files in scope in a Markdown document:
mdData := Data{
Context: ctx,
Doc: mdata.Variables,
Styles: c.Styles,
Scripts: c.Scripts,
Meta: meta,
Files: files,
}
So .Files probably works in Caddyfile templates but won't work in a Markdown template. Is this a intentional or a bug?
If this is the desired behavior then the documentation should probably be updated.
@ericdreeves Do you want to look into this? ^
So yeah, if I change my usage to {{.Context.Files "."}} then I get what I would expect to get. The easiest way to resolve this is probably to just change the following paragraph in the documentation to note that the .Context prefix is required inside Markdown templates:
Along with these template actions, all the standard Caddy template actions are available to you in Markdown templates. Be sure to sanitize anything you render as HTML (use the html, js, and urlquery functions)!
But I'll obviously leave it up to you all to decide what the desired behavior is 馃槂
@georgelesica-wf That's a good point. The .Context prefix is required to disambiguate between the Files method on the Data struct or the promoted Files method on the Context. Let's just update the docs. Nevermind, @ericdreeves! ;) I'll do this in a moment.