Template ListFiles result ordering

1. My Caddy version (caddy -version):

Caddy 0.11.0 (+1f7b5ab Thu May 10 15:51:18 UTC 2018) (unofficial)
1 file changed, 1 insertion(+), 1 deletion(-)
caddy/caddymain/run.go

2. How I run Caddy:

a. System environment:

OS, relevant versions, systemd? docker? etc.
Ubuntu 18.04.3 LTS, systemd

3. The problem I’m having:

I would like to use Caddy’s Template function .Context.ListFiles to order the results in user-controlled ways. For my specific current need, I’d like to order the files by date created, newest first.

4. Error messages and/or full log output:

No errors. It seems that .Context.ListFiles doesn’t provide me any arguments or mechanisms to control the ordering of the list of files.

5. What I already tried:

I’ve tried

<ul>
{{range $name := .Context.Files "." }}
<li>{{ $name }}</li>
{{end}}

and that does give me a list of file names; but it doesn’t give me anything beyond the file name. So I am unable to sort the list by creation date.

6. Links to relevant resources:

The files are just listed in the order that the OS provides them: tplcontext.go - caddyserver/caddy - Sourcegraph

This is faster on large file listings.

You could draft a proposal on sprig and see if they’d be willing to accept a function to sort arbitrary slices (I’m not sure how that would work though, off-hand).

Aye, thanks. My use case is pretty narrow, and can be reasonably solved outside of Caddy template with a little shell magic on my source data. I was just hoping there might be some undocumented sorting available to me.

Thanks!

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