Understanding the Import Template

1. The problem I’m having:

Hi all. I’ve been trying to wrap my head around using the Import template to include a header.html file in my other static HTML files. Whenever I try to add {{ include /path/to/htmlFile }} to a page, I get 500 internal server errors from that page.

I’ve gone through a few import-related forum posts and the GitHub demo files and still can’t seem to get it working.

2. Error messages and/or full log output:

Journalctl Results

Sep 11 20:59:15 host-77b2e2c systemd[1]: Starting Caddy...
Sep 11 20:59:15 host-77b2e2c caddy[30420]: caddy.HomeDir=/var/lib/caddy
Sep 11 20:59:15 host-77b2e2c caddy[30420]: caddy.AppDataDir=/var/lib/caddy/.local/share/caddy
Sep 11 20:59:15 host-77b2e2c caddy[30420]: caddy.AppConfigDir=/var/lib/caddy/.config/caddy
Sep 11 20:59:15 host-77b2e2c caddy[30420]: caddy.ConfigAutosavePath=/var/lib/caddy/.config/caddy/autosave.json
Sep 11 20:59:15 host-77b2e2c caddy[30420]: caddy.Version=v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=
Sep 11 20:59:15 host-77b2e2c caddy[30420]: runtime.GOOS=linux
Sep 11 20:59:15 host-77b2e2c caddy[30420]: runtime.GOARCH=amd64
Sep 11 20:59:15 host-77b2e2c caddy[30420]: runtime.Compiler=gc
Sep 11 20:59:15 host-77b2e2c caddy[30420]: runtime.NumCPU=1
Sep 11 20:59:15 host-77b2e2c caddy[30420]: runtime.GOMAXPROCS=1
Sep 11 20:59:15 host-77b2e2c caddy[30420]: runtime.Version=go1.22.3
Sep 11 20:59:15 host-77b2e2c caddy[30420]: os.Getwd=/
Sep 11 20:59:15 host-77b2e2c caddy[30420]: LANG=en_US.UTF-8
Sep 11 20:59:15 host-77b2e2c caddy[30420]: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
Sep 11 20:59:15 host-77b2e2c caddy[30420]: NOTIFY_SOCKET=/run/systemd/notify
Sep 11 20:59:15 host-77b2e2c caddy[30420]: HOME=/var/lib/caddy
Sep 11 20:59:15 host-77b2e2c caddy[30420]: LOGNAME=caddy
Sep 11 20:59:15 host-77b2e2c caddy[30420]: USER=caddy
Sep 11 20:59:15 host-77b2e2c caddy[30420]: INVOCATION_ID=57e51a1600524b528797825a79bd9350
Sep 11 20:59:15 host-77b2e2c caddy[30420]: JOURNAL_STREAM=8:463648
Sep 11 20:59:15 host-77b2e2c caddy[30420]: SYSTEMD_EXEC_PID=30420
Sep 11 20:59:15 host-77b2e2c caddy[30420]: GOTRACEBACK=none
Sep 11 20:59:15 host-77b2e2c caddy[30420]: {"level":"info","ts":1726088355.8477995,"msg":"using config from file","file":"/etc/caddy/Caddyfile"}
Sep 11 20:59:15 host-77b2e2c caddy[30420]: {"level":"info","ts":1726088355.8515468,"msg":"adapted config to JSON","adapter":"caddyfile"}
Sep 11 20:59:15 host-77b2e2c caddy[30420]: {"level":"warn","ts":1726088355.851805,"msg":"Caddyfile input is not formatted; run 'caddy fmt --overwrite' to fix inconsistencies","adapter":"caddyfile","file":"/etc/caddy/Caddyfile","line":10}
Sep 11 20:59:15 host-77b2e2c caddy[30420]: {"level":"info","ts":1726088355.8537524,"msg":"redirected default logger","from":"stderr","to":"/var/log/caddy/mysite.tld.log"}
Sep 11 20:59:15 host-77b2e2c caddy[30420]: {"level":"info","ts":1726088355.857897,"msg":"failed to sufficiently increase receive buffer size (was: 208 kiB, wanted: 7168 kiB, got: 416 kiB). See https://github.com/quic-go/quic-go/wiki/UDP-Buffer-Sizes for details."}
Sep 11 20:59:15 host-77b2e2c systemd[1]: Started Caddy.

Curl -vL Results

PS Microsoft.PowerShell.Core\FileSystem::C:\> curl -vL https://mysite.tld/pages/test
*   Trying my.ip.address:443...
* Connected to mysite.tld (my.ip.address) port 443 (#0)
* schannel: disabled automatic use of client certificate
* ALPN: offers http/1.1
* ALPN: server accepted http/1.1
* using HTTP/1.1
> GET /pages/test HTTP/1.1
> Host: mysite.tld
> User-Agent: curl/8.0.1
> Accept: */*
>
* schannel: server closed the connection
< HTTP/1.1 500 Internal Server Error
< Accept-Ranges: bytes
< Alt-Svc: h3=":443"; ma=2592000
< Content-Length: 119
< Content-Type: text/html; charset=utf-8
< Etag: "d44clw8j9gsn3b"
< Last-Modified: Thu, 12 Sep 2024 13:43:12 GMT
< Server: Caddy
< Vary: Accept-Encoding
< Date: Thu, 12 Sep 2024 13:56:33 GMT
<
500 Internal Server Error* schannel: server indicated shutdown in a prior call
* transfer closed with 94 bytes remaining to read
* Closing connection 0
* schannel: shutting down SSL/TLS connection with mysite.tld port 443
curl: (18) transfer closed with 94 bytes remaining to read

3. Caddy version:

v2.8.4

4. How I installed and ran Caddy:

a. System environment:

Ubuntu 22.04.4 LTS

b. Command:

caddy (start/reload)

c. HTML files:

The below files are as they are now, but I’ve tried several different ways to make it work. Same problem every time.

header.html

{{ define "header"}}
<nav class="w3-bar w3-black">
   <a href="index" class="w3-button w3-bar-item">Home</a>
   <a href="targets" class="w3-button w3-bar-item">Targets</a>
   <a href="merits" class="w3-button w3-bar-item">Merits</a>
</nav>
{{ end }}

test.html. Deleting {{ … }} resolves error 500.

<!DOCTYPE html>
<html>
   <body>
      {{ import "/var/www/mysite.tld/pages/header.html" }}
      Hello world
   </body>
</html>

d. My complete Caddy config:

mysite.tld {
        root * /var/www/mysite.tld
        file_server {
                index ./pages/home.html
                hide .git
        }
        templates
        try_files {path} {path}/ {path}.html
        handle_errors {
                respond "{err.status_code} {err.status_text}"
        }
}

Your Caddy logs should show errors if it fails to run the template.

I think import is meant to be relative to the root, so you should do:

{{ import "/pages/header.html" }}
2 Likes

PROBLEM RESOLVED

Oh my god it really was that simple.

Thanks so much for your help both here and in every single other forum thread I’ve checked.


For anyone else dealing with this issue, my working HTML files were as follows:

header.html

{{ define "header"}}
<nav class="w3-bar w3-black">
   <a href="index" class="w3-button w3-bar-item">Home</a>
   <a href="targets" class="w3-button w3-bar-item">Targets</a>
   <a href="merits" class="w3-button w3-bar-item">Merits</a>
</nav>
{{ end }}

test.html

 {{ import "/pages/header.html" }}
   <!DOCTYPE html>
   <html>
      <body>
         {{ template "header" }}
         Hello world
      </body>
   </html>
1 Like

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