MCPcopy
hub / github.com/caddyserver/caddy / executeTemplate

Method executeTemplate

modules/caddyhttp/templates/templates.go:483–510  ·  modules/caddyhttp/templates/templates.go::Templates.executeTemplate

executeTemplate executes the template contained in wb.buf and replaces it with the results.

(rr caddyhttp.ResponseRecorder, r *http.Request)

Source from the content-addressed store, hash-verified

481
482// executeTemplate executes the template contained in wb.buf and replaces it with the results.
483func (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.

Callers 1

ServeHTTPMethod · 0.95

Calls 6

ErrorFunction · 0.92
ReplaceAllMethod · 0.80
BufferMethod · 0.65
ValueMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected