diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2023-10-24 07:20:10 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2023-10-24 07:20:29 +0200 |
| commit | 18476c6cba2d5d2bb3ecf3ad136dacbf41d0360e (patch) | |
| tree | a7a958ff7a4bf14ed7f7ee3ffd9c84b7592facbd /core/http.go | |
feat: Extract from other repo
Diffstat (limited to 'core/http.go')
| -rw-r--r-- | core/http.go | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/core/http.go b/core/http.go new file mode 100644 index 0000000..778c5c9 --- /dev/null +++ b/core/http.go @@ -0,0 +1,28 @@ +package core + +import ( + "bytes" + "context" + "html/template" + "net/http" + + "github.com/a-h/templ" +) + +func ExecuteTemplate(w http.ResponseWriter, tpl template.Template, name string, data interface{}) { + var buf bytes.Buffer + err := tpl.ExecuteTemplate(&buf, name, data) + Expect(err, "error executing template") + w.Header().Set("Content-Type", "text/html; charset=UTF-8") + _, err = buf.WriteTo(w) + Expect(err, "error writing template to response writer") +} + +func RenderPage(ctx context.Context, page templ.Component, w http.ResponseWriter) { + var buf bytes.Buffer + err := page.Render(ctx, &buf) + Expect(err, "error rendering page") + w.Header().Set("Content-Type", "text/html; charset=UTF-8") + _, err = buf.WriteTo(w) + Expect(err, "error writing page to response writer") +} |
