executeTemplate executes the template contained in wb.buf and replaces it with the results.
(rr caddyhttp.ResponseRecorder, r *http.Request)
| 481 | |
| 482 | // executeTemplate executes the template contained in wb.buf and replaces it with the results. |
| 483 | func (t *Templates) executeTemplate(rr caddyhttp.ResponseRecorder, r *http.Request) error { |
| 484 | var fs http.FileSystem |
| 485 | if t.FileRoot != "" { |
| 486 | repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer) |
| 487 | fs = http.Dir(repl.ReplaceAll(t.FileRoot, ".")) |
| 488 | } |
| 489 | |
| 490 | ctx := &TemplateContext{ |
| 491 | Root: fs, |
| 492 | Req: r, |
| 493 | RespHeader: WrappedHeader{rr.Header()}, |
| 494 | config: t, |
| 495 | CustomFuncs: t.customFuncs, |
| 496 | } |
| 497 | |
| 498 | err := ctx.executeTemplateInBuffer(r.URL.Path, rr.Buffer()) |
| 499 | if err != nil { |
| 500 | // templates may return a custom HTTP error to be propagated to the client, |
| 501 | // otherwise for any other error we assume the template is broken |
| 502 | var handlerErr caddyhttp.HandlerError |
| 503 | if errors.As(err, &handlerErr) { |
| 504 | return handlerErr |
| 505 | } |
| 506 | return caddyhttp.Error(http.StatusInternalServerError, err) |
| 507 | } |
| 508 | |
| 509 | return nil |
| 510 | } |
| 511 | |
| 512 | // virtualResponseWriter is used in virtualized HTTP requests |
| 513 | // that templates may execute. |
no test coverage detected