bagbag
(Patrick)
April 24, 2020, 7:38pm
1
1. My Caddy version (caddy version
):
v2.0.0-rc.3
2. Caddyfile
(file_server to create a 404)
http://localhost
file_server
handle_errors {
rewrite * /error-page.html
file_server { root /templates }
templates
}
3. The problem I’m having:
I want to create custom error pages and therefore require placeholders like {http.error.status_code}
.
The template is a simple html file having these placeholders in it.
The page is rendered having the raw placeholder instead of the value.
4. What I already tried:
{{ placeholder }}
{{ .placeholder }}
{ placeholder }
5. Links to relevant resources:
https://caddyserver.com/docs/modules/http.handlers.templates
I don’t think that’s supported yet. Templates will only be improved with time!
What you can do instead for now, is to have separate matchers for each error code which rewrite to different pages:
handle_errors {
@404 {
expression {http.error.status_code} == 404
}
rewrite @404 /404.html
file_server { root /templates }
templates
}
You could also set a status code in each of those error-code specific pages like this: {{ $status_code := 404 }}
, then use {{ include "/error_template.html" }}
, etc. This would help avoid some duplication in your error page contents.
1 Like
matt
(Matt Holt)
April 25, 2020, 9:54pm
3
@bagbag That’s a good idea, can you request this as a feature on GitHub so we can track its development?
bagbag
(Patrick)
April 26, 2020, 10:38pm
4
@francislavoie
thanks, that’s what I gonna do, until this feature is implemented.
@matt
of course, thank you for caddy!
opened 10:37PM - 26 Apr 20 UTC
closed 11:17PM - 20 Jul 20 UTC
feature
good first issue
(Feature request out of <https://caddy.community/t/use-placeholders-in-templates… /7697>)
I would like to see the ability of using placeholders like `{http.error.status_code}` in templates. This would allow us, for example, to create a single custom error page in nginx style. For now we have to create a page for every status code we would like to support (maybe using `respond` with include and arguments would work too, but I haven't tested that).
Instead of adding an extra layer of complexity to substitute placeholders in templates, my suggestion is to either implement a new template function like `{{.Placerholder "http.error.status_code"}}` (with a better name than `.Placeholder`) or if variables in Go templates work the way I guess they work, populate them (if that makes sense, I have no idea about Go templates yet). But I think a function is the better solution.
When this is implemented, caddys template functions `.RemoteIP` and `.Host` could be deprecated and later removed, as the placeholders includes them with `http.request.remote.host` and `http.request.host`.
I guess this feature would fit for caddy v2.1. I have no idea what the release schedule for caddy looks like, but if it takes a few days or more from now until the release of 2.1, I would like to try to implement this feature, as it is probably quite easy to implement and therefore a good candidate for me to get started with Go and contribute to this great project (hints welcomed).
2 Likes
system
(system)
Closed
May 26, 2020, 10:38pm
5
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.