Use placeholders in templates

1. My Caddy version (caddy version):


2. Caddyfile

(file_server to create a 404)



handle_errors {
  rewrite * /error-page.html
  file_server { root /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:

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 }

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?

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

of course, thank you for caddy!


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