Use placeholders in templates

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

@bagbag That’s a good idea, can you request this as a feature on GitHub so we can track its development?

@francislavoie
thanks, that’s what I gonna do, until this feature is implemented.

@matt
of course, thank you for caddy!

2 Likes

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