Using template_file in file_server browse

Just need a better explanation of how to use template_file with file_server and browse

my stanza is

https://example.x.net {
  import r53
  file_server {
    root "/data/eval/filerun/user-files/"
    hide .*
    browse
  }
}

that works fine. now I want to use my own template to brand the page a bit so I copied and modfied the html template from the link in the docs https://github.com/caddyserver/caddy/blob/master/modules/caddyhttp/fileserver/browsetpl.go. Then I tried adding what I thought was a directive template_file but caddy says it’s not. So obvioiusly I don’t understand what the carrots around <template_file> in the docs are trying to tell me about how to use it.

this throws an error
run: adapting config using caddyfile: parsing caddyfile tokens for 'file_server': /opt/caddy/conf/giskard/caddy.conf:47 - Error during parsing: unknown subdirective 'template_file'

https://example.x.net {
  import r53
  file_server {
    root "/data/eval/filerun/user-files/"
    hide .*
    browse
    template_file /opt/caddy/templates/browser.html
  }
}

1. Caddy version (caddy version):

v2.4.5 h1:P1mRs6V2cMcagSPn+NWpD+OEYUYLIf6ecOa48cFGeUg=
compiled with xcaddy with route53 plugin

1 Like

The syntax is browse [<template_file>], not template_file <filename>:

The [ ] indicate optional parameter: Caddyfile Directives — Caddy Documentation

Oh now I see. Ok now it works great.

So what threw me was <template_file> in the bullet list below having its own bullet when it only applies to browse. Having it’s own bullet made me think it was a directive of file_server as all the rest of the bullets are. I’d suggest moving that template_file bullet text under the browse bullet. Now I know to pay more attention to the primary directive syntax and not the bullet list.

They’re listed in order of appearance in the syntax block. browse can be both specified as an argument (as a shortcut) and as a subdirective.

This topic was automatically closed after 30 days. New replies are no longer allowed.