1. Caddy version (caddy version
):
v2.4.2 h1:chB106RlsIaY4mVEyq9OQM5g/9lHYVputo/LAX2ndFg=
2. How I run Caddy:
a. System environment:
Archlinux
b. Command:
sudo ./caddy run --config Caddyfile2
d. My complete Caddyfile or JSON config:
Caddyfile2:
{
debug
}
*:80 {
log {
level debug
output stdout
}
root * html
templates
file_server
}
3. The problem I’m having:
I am building an html site with templating. But when I include a file, I seem not to be able to define templates in one file call it in another one. This is the case in both directions (defining in the including, calling in the included, and vice versa). For instance, I have two files in a subdirectory of my html root, index.html
and include.html
:
W0002/include.html:
{{define "footer"}}
<div>
<p>baz</p>
</div>
{{end}}
bar
W0002/index.html:
{{$title := "Foo" }}
{{include "W0002/include.html"}}
<!DOCTYPE html>
<html>
<head>
<title>{{$title}} — Test Templating</title>
</head>
<body>
<main>
<h1>{{$title}}</h1>
</main>
{{template "footer"}}
</body>
</html>
When I call W0002/index.html in the browser, I get a 500 error and the log below.
4. Error messages and/or full log output:
2021/06/18 06:51:53.064 DEBUG http.handlers.file_server sanitized path join {"site_root": "html", "request_path": "/W0002/index.html", "result": "html/W0002/index.html"}
2021/06/18 06:51:53.064 DEBUG http.handlers.file_server opening file {"filename": "html/W0002/index.html"}
2021/06/18 06:51:53.065 ERROR http.log.error.log0 template: /W0002/index.html:13:13: executing "/W0002/index.html" at <{{template "footer"}}>: template "footer" not defined {"request": {"remote_addr": "[::1]:54456", "proto": "HTTP/1.1", "method": "GET", "host": "localhost", "uri": "/W0002/index.html", "headers": {"Connection": ["keep-alive"], "Sec-Fetch-User": ["?1"], "Sec-Fetch-Mode": ["navigate"], "Sec-Fetch-Dest": ["document"], "Accept-Encoding": ["gzip, deflate, br"], "User-Agent": ["Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Pragma": ["no-cache"], "Cache-Control": ["no-cache"], "Sec-Ch-Ua": ["\"Chromium\";v=\"91\", \" Not;A Brand\";v=\"99\""], "Sec-Ch-Ua-Mobile": ["?0"], "Dnt": ["1"], "Upgrade-Insecure-Requests": ["1"], "Sec-Fetch-Site": ["none"], "Accept-Language": ["de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7,es;q=0.6,fr;q=0.5,pt;q=0.4"]}}, "duration": 0.000869988, "status": 500, "err_id": "jg5vgs2vm", "err_trace": "templates.(*Templates).executeTemplate (templates.go:315)"}
2021/06/18 06:51:53.065 ERROR http.log.access.log0 handled request {"request": {"remote_addr": "[::1]:54456", "proto": "HTTP/1.1", "method": "GET", "host": "localhost", "uri": "/W0002/index.html", "headers": {"Sec-Fetch-User": ["?1"], "Connection": ["keep-alive"], "Accept-Encoding": ["gzip, deflate, br"], "Sec-Fetch-Mode": ["navigate"], "Sec-Fetch-Dest": ["document"], "Sec-Ch-Ua": ["\"Chromium\";v=\"91\", \" Not;A Brand\";v=\"99\""], "Sec-Ch-Ua-Mobile": ["?0"], "Dnt": ["1"], "Upgrade-Insecure-Requests": ["1"], "User-Agent": ["Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Pragma": ["no-cache"], "Cache-Control": ["no-cache"], "Sec-Fetch-Site": ["none"], "Accept-Language": ["de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7,es;q=0.6,fr;q=0.5,pt;q=0.4"]}}, "common_log": "::1 - - [18/Jun/2021:08:51:53 +0200] \"GET /W0002/index.html HTTP/1.1\" 500 0", "duration": 0.000869988, "size": 0, "status": 500, "resp_headers": {"Server": ["Caddy"], "Etag": ["\"quvxq66q\""], "Content-Type": ["text/html; charset=utf-8"], "Last-Modified": ["Fri, 18 Jun 2021 06:51:42 GMT"], "Accept-Ranges": ["bytes"], "Content-Length": ["242"]}}
2021/06/18 06:51:53.065 ERROR http.log.access.log0 handled request {"request": {"remote_addr": "[::1]:54456", "proto": "HTTP/1.1", "method": "GET", "host": "localhost", "uri": "/W0002/index.html", "headers": {"User-Agent": ["Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Pragma": ["no-cache"], "Cache-Control": ["no-cache"], "Sec-Ch-Ua": ["\"Chromium\";v=\"91\", \" Not;A Brand\";v=\"99\""], "Sec-Ch-Ua-Mobile": ["?0"], "Dnt": ["1"], "Upgrade-Insecure-Requests": ["1"], "Sec-Fetch-Site": ["none"], "Accept-Language": ["de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7,es;q=0.6,fr;q=0.5,pt;q=0.4"], "Connection": ["keep-alive"], "Sec-Fetch-User": ["?1"], "Sec-Fetch-Mode": ["navigate"], "Sec-Fetch-Dest": ["document"], "Accept-Encoding": ["gzip, deflate, br"]}}, "common_log": "::1 - - [18/Jun/2021:08:51:53 +0200] \"GET /W0002/index.html HTTP/1.1\" 500 0", "duration": 0.000869988, "size": 0, "status": 500, "resp_headers": {"Accept-Ranges": ["bytes"], "Content-Length": ["242"], "Server": ["Caddy"], "Etag": ["\"quvxq66q\""], "Content-Type": ["text/html; charset=utf-8"], "Last-Modified": ["Fri, 18 Jun 2021 06:51:42 GMT"]}}
5. What I already tried:
When I comment out the call {{template "footer"}}
, I get the expected result:
bar
Foo
where “bar” is from the included file and ends up right before the doctype definition:
bar
<!DOCTYPE html>
<html>
<head>
<title>Foo — Test Templating</title>
</head>
<body>
<main>
<h1>Foo</h1>
</main>
</body>
</html>
(Also, the {{define "footer"}}
does have the effect that the “baz part” does not end up in the resulting html, only those parts of the included file that are not in a define
block.)
So, to me, this looks like templating as such is working, only calling templates defined elsewhere does not.
For context, what I actually want to achieve is this (maybe there are ways to do it better?):
- Call a specific page, say
W0002/abc.html
, - in this page, define variables and an
inner
template that are required for rendering specific contents, - include code that is the same for
W0002/abc.html
,W0002/def.html
,W0003/xyz.html
from a templatetemplates/base.html
. This contains the doctype definition and top-level<html>
tags. At one point, it calls aninner
template defined inW0002/abc.html
, - render everything at the
W0002/abc.html
url.
Now, either I {{define "outer"}}
content in templates/base.html
and call it with {{template "outer"}}
in W0002/abc.html
, which gives me the error as above.
Or I directly {{include "templates/base.html"}}
the general stuff without putting it in a template definition. Then it errors out when this general stuff in templates/base.html
calls the {{template "inner"}}
, because that’s defined in where I started from, W0002/abc.html
.