You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rethink - by creating a separate ComponentHandler struct and a templ.Handler function that creates one, it's possible to support adding a status code without having developers deal with middleware.
// ComponentHandler is a http.Handler that renders components.typeComponentHandlerstruct {
ComponentComponentStatusintErrorHandler http.Handler
}
// ServeHTTP implements the http.Handler interface.func (chComponentHandler) ServeHTTP(w http.ResponseWriter, r*http.Request) {
ifch.Status!=0 {
w.WriteHeader(ch.Status)
}
iferr:=ch.Component.Render(r.Context(), w); err!=nil {
ifch.ErrorHandler!=nil {
ch.ErrorHandler.ServeHTTP(w, r)
return
}
http.Error(w, "templ: failed to render template", http.StatusInternalServerError)
}
}
// Handler creates a http.Handler that renders the template.funcHandler(cComponent) ComponentHandler {
returnComponentHandler{
Component: c,
}
}
At the moment, to output a template over HTTP, the template has to be wrapped by some boilerplate code.
If templ components implemented
http.Handler
, it could be shortened to:The component's
ServeHTTP
method would render a minimal error page, and return a 500 status code if the template failed to render.In the case that the author of the template wanted to return a non HTTP 200 status code etc. then they'd use HTTP middleware to do it.
The text was updated successfully, but these errors were encountered: