Refactor html templates

This commit entirely refactors the use of html templates. Instead of
inheriting from a shared template, we insert the results into a base
template.
This commit is contained in:
Tom Wiesing 2023-01-20 14:42:37 +01:00
parent 6ede99d7c6
commit d235ee4e5c
No known key found for this signature in database
59 changed files with 869 additions and 777 deletions

View file

@ -49,10 +49,11 @@ func StatusInterceptor(contentType string, body func(code int, text string) ([]b
return ErrInterceptor{
Errors: map[error]Response{
ErrBadRequest: makeResponse(http.StatusBadRequest),
ErrNotFound: makeResponse(http.StatusNotFound),
ErrForbidden: makeResponse(http.StatusForbidden),
ErrMethodNotAllowed: makeResponse(http.StatusMethodNotAllowed),
ErrInternalServerError: makeResponse(http.StatusInternalServerError),
ErrBadRequest: makeResponse(http.StatusBadRequest),
ErrNotFound: makeResponse(http.StatusNotFound),
ErrForbidden: makeResponse(http.StatusForbidden),
ErrMethodNotAllowed: makeResponse(http.StatusMethodNotAllowed),
},
Fallback: makeResponse(http.StatusInternalServerError),
}
@ -60,10 +61,11 @@ func StatusInterceptor(contentType string, body func(code int, text string) ([]b
// Common errors accepted by all httpx handlers
var (
ErrBadRequest = errors.New("httpx: Bad Request")
ErrNotFound = errors.New("httpx: Not Found")
ErrForbidden = errors.New("httpx: Forbidden")
ErrMethodNotAllowed = errors.New("httpx: Method Not Allowed")
ErrInternalServerError = errors.New("httpx: Internal Server Error")
ErrBadRequest = errors.New("httpx: Bad Request")
ErrNotFound = errors.New("httpx: Not Found")
ErrForbidden = errors.New("httpx: Forbidden")
ErrMethodNotAllowed = errors.New("httpx: Method Not Allowed")
)
var (

View file

@ -5,6 +5,8 @@ import (
"net/http"
"strings"
_ "embed"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx/field"
"github.com/gorilla/csrf"
)
@ -173,3 +175,9 @@ func (form *Form[D]) renderSuccess(data D, values map[string]string, w http.Resp
}
form.renderForm(err, values, w, r)
}
//go:embed "form.html"
var formBytes []byte
// FormTeplate is a template to embed a form
var FormTemplate = template.Must(template.New("form.html").Parse(string(formBytes)))

22
pkg/httpx/form.html Normal file
View file

@ -0,0 +1,22 @@
<div class="pure-u-1">
{{ block "form/extra" . }}<!-- no extra -->{{ end }}
<form class="pure-form pure-form-aligned" method="POST">
<fieldset>
{{ block "form/message" . }}
{{ $E := .Error }}
{{ if not (eq $E "") }}
<div class="pure-form-group">
<p class="error-message">
{{ $E }}
</p>
</div>
{{ end }}
{{ end }}
{{ block "form/inside" . }}<!-- no inside -->{{ end }}
{{ .Form }}
<input type="submit" value="{{ block "form/button" .}}Submit{{ end }}" class="pure-button">
</fieldset>
</form>
</div>