BenStigsen
(Benjamin Stigsen)
March 3, 2022, 1:56am
1
1. Caddy version (caddy version
):
v2.4.7-0.20220219223636-186fdba916a1
2. How I run Caddy:
caddy run
a. System environment:
Solus OS (latest)
What is going wrong:
So I have the following file:
test.html:
<html>
{{include "/templates/head.html" "mycoolwebsite"}}
<body>
</body>
</html>
head.html:
<head>
<meta charset="utf-8">
<title>{{.Args 0}} | Wow!</title>
</head>
What I’m expecting:
When I go to localhost:443/test.html
I expect it to show a page, with a window where the title is “mycoolwebsite”
What is happening:
2022/03/03 01:50:14.067 ERROR http.log.error template: /company:2:4: executing "/test.html" at <include "/templates/head.html" "mycoolwebsite">: error calling include: template: /templates/head.html:4:11: executing "/templates/head.html" at <.Args>: Args has arguments but cannot be invoked as function {"request": {"remote_ip": "127.0.0.1", "remote_port": "45226", "proto": "HTTP/2.0", "method": "GET", "host": "localhost", "uri": "/test.html", "headers": {"User-Agent": ["curl/7.79.1"], "Accept": ["*/*"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "server_name": "localhost"}}, "duration": 0.001573409, "status": 500, "err_id": "at68kezth", "err_trace": "templates.(*Templates).executeTemplate (templates.go:347)"}
It is not clear to me what this means:
executing “/templates/head.html” at <.Args>: Args has arguments but cannot be invoked as function
BenStigsen
(Benjamin Stigsen)
March 3, 2022, 2:05am
3
Doing {{index .Args 0}}
fixed it.
But then how would I do something like:
{{include "/templates/head.html" .URL.Query.Get "name"}}
1 Like
matt
(Matt Holt)
March 3, 2022, 6:43am
4
Yeah, our docs are wrong. .Args is an array, so it needs to be indexed into, not called. Nice job figuring it out! I’ll fix the docs.
I don’t fully understand the next question though?
BenStigsen
(Benjamin Stigsen)
March 3, 2022, 8:45pm
5
It’s because {{include "somefile.html"}}
takes some optional arguments. So I’d like to pass the name
value from the URL, to the included file.
mywebsite.com?name=benjamin
Then I’d like to pass benjamin
to the included file, so that I can use index .Args 0
in the included file to get benjamin
.
This however does not seem to work:
{{include "/templates/head.html" .URL.Query.Get "name"}}
I pretty much never play with Go templates so I don’t know if this is right, but maybe you need to do this?
{{include "/templates/head.html" (.URL.Query.Get "name")}}
BenStigsen
(Benjamin Stigsen)
March 4, 2022, 2:21am
7
That does not work for some reason. I’m not sure what to do.
2022/03/04 02:19:26.471 ERROR http.log.error template: /mywebsite:2:40: executing "/mywebsite" at <.URL.Query.Get>: can't evaluate field URL in type *templates.TemplateContext {"request": {"remote_ip": "127.0.0.1", "remote_port": "33022", "proto": "HTTP/2.0", "method": "GET", "host": "localhost", "uri": "/mywebsite?name=test", "headers": {"User-Agent": ["curl/7.79.1"], "Accept": ["*/*"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "server_name": "localhost"}}, "duration": 0.00135977, "status": 500, "err_id": "34nhb8ueq", "err_trace": "templates.(*Templates).executeTemplate (templates.go:347)"}
BenStigsen
(Benjamin Stigsen)
March 4, 2022, 2:24am
8
Actually, this in an of itself does not seem to work for me:
<h1>{{.URL.Query.Get "name"}}</h1>
I think you need to do .Req.Url.Query.Get
, because the URL is inside the request
2 Likes
BenStigsen
(Benjamin Stigsen)
March 4, 2022, 3:14am
10
Yep that did it. Thank you!
1 Like
system
(system)
Closed
April 2, 2022, 1:57am
11
This topic was automatically closed after 30 days. New replies are no longer allowed.