caddyhttp: Implement placeholders in templates

Created on 27 Apr 2020  路  5Comments  路  Source: caddyserver/caddy

(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).

feature request good first issue

Most helpful comment

When this is implemented, caddys template functions .RemoteIP and .Host could be deprecated and later removed

I don't think this is necessary, shorter/easier APIs are beneficial.

I guess this feature would fit for caddy v2.1. I have no idea what the release schedule for caddys 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).

v2.1 is still a while away, v2.0 stable isn't even out yet! But yes, this would be the right version to target for this change.

The files you're looking for are in /modules/caddyhttp/templates/ 馃憤 Good luck!

All 5 comments

When this is implemented, caddys template functions .RemoteIP and .Host could be deprecated and later removed

I don't think this is necessary, shorter/easier APIs are beneficial.

I guess this feature would fit for caddy v2.1. I have no idea what the release schedule for caddys 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).

v2.1 is still a while away, v2.0 stable isn't even out yet! But yes, this would be the right version to target for this change.

The files you're looking for are in /modules/caddyhttp/templates/ 馃憤 Good luck!

I assume you mean .Req.RemoteIP and .Req.Host?

We can't really get rid of those since they're on the standard lib's http.Request struct.

We could expose a way to access replacer values from the templates instead; it just involves getting a context value out of the request, type-asserting it, then calling Get() on the Replacer type.

No he means .RemoteIP and .Host as documented here: https://caddyserver.com/docs/modules/http.handlers.templates

Oh, I think that's pretty old docs. It'll be updated at 2.0.

My bad -- those are actually different.

Was this page helpful?
0 / 5 - 0 ratings