From 465e88bf4088cb0633c18e53b40863bd4de15b95 Mon Sep 17 00:00:00 2001 From: Zeni Kim Date: Fri, 27 Sep 2024 02:27:24 -0500 Subject: [PATCH] theme base system --- core.go | 11 ++++----- response.go | 15 ++++++++++++ template.go | 45 ---------------------------------- template/components/head.html | 8 ++++++ template/components/title.go | 5 ++++ template/components/title.html | 5 ++++ templates.go | 36 +++++++++++++++++++++++++++ 7 files changed, 74 insertions(+), 51 deletions(-) delete mode 100644 template.go create mode 100644 template/components/head.html create mode 100644 template/components/title.go create mode 100644 template/components/title.html create mode 100644 templates.go diff --git a/core.go b/core.go index d9093e1..ba208d3 100644 --- a/core.go +++ b/core.go @@ -39,7 +39,7 @@ var basePath string var disableEvents bool = false //go:embed all:template -var resources embed.FS +var components_resources embed.FS type configContainer struct { Request RequestConfig @@ -79,6 +79,10 @@ func (app *App) Bootstrap() { NewEventsManager() } +func (app *App) RegisterTemplates(templates_resources embed.FS) { + NewTemplates(components_resources, templates_resources) +} + func (app *App) Run(router *httprouter.Router) { portNumber := os.Getenv("App_HTTP_PORT") if portNumber == "" { @@ -97,11 +101,6 @@ func (app *App) Run(router *httprouter.Router) { // add public path publicPath := os.Getenv("TEMPLATE_PUBLIC") router.ServeFiles("/public/*filepath", http.Dir(publicPath)) - - // templates - var template = new(Template) - template.NewTemplates(resources) - } useHttpsStr := os.Getenv("App_USE_HTTPS") diff --git a/response.go b/response.go index 1b869df..7c03b15 100644 --- a/response.go +++ b/response.go @@ -6,6 +6,7 @@ package core import ( + "bytes" "fmt" "net/http" ) @@ -63,6 +64,20 @@ func (rs *Response) HTML(body string) *Response { return rs } +// TODO add doc +func (rs *Response) Template(name string, data interface{}) *Response { + var buffer bytes.Buffer + if rs.isTerminated == false { + err := tmpl.ExecuteTemplate(&buffer, name, data) + if err != nil { + panic(fmt.Sprintf("error executing template: %v", err)) + } + rs.contentType = CONTENT_TYPE_HTML + buffer.WriteTo(rs.HttpResponseWriter) + } + return rs +} + // TODO add doc func (rs *Response) SetStatusCode(code int) *Response { if rs.isTerminated == false { diff --git a/template.go b/template.go deleted file mode 100644 index a0724d7..0000000 --- a/template.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) 2024 Zeni Kim -// Use of this source code is governed by MIT-style -// license that can be found in the LICENSE file. - -package core - -import ( - "bytes" - "embed" - "fmt" - "html/template" - "io/fs" - "net/http" - "strings" -) - -type Template struct { - tmpl *template.Template -} - -func (t *Template) NewTemplates(resources embed.FS) { - var paths []string - fs.WalkDir(resources, ".", func(path string, d fs.DirEntry, err error) error { - if strings.Contains(d.Name(), ".html") { - paths = append(paths, path) - } - return nil - }) - - t.tmpl = template.Must(template.ParseFS(resources, paths...)) -} - -func (t *Template) Render(w http.ResponseWriter, name string, data interface{}) { - var buffer bytes.Buffer - - err := t.tmpl.ExecuteTemplate(&buffer, name, data) - if err != nil { - err = fmt.Errorf("error executing template: %w", err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", "text/html; charset=UTF-8") - buffer.WriteTo(w) -} diff --git a/template/components/head.html b/template/components/head.html new file mode 100644 index 0000000..cecc5ed --- /dev/null +++ b/template/components/head.html @@ -0,0 +1,8 @@ +{{define "head"}} + + + + {{.}} | Goffee + + +{{end}} \ No newline at end of file diff --git a/template/components/title.go b/template/components/title.go new file mode 100644 index 0000000..0e68b6a --- /dev/null +++ b/template/components/title.go @@ -0,0 +1,5 @@ +package components + +type Title struct { + Label string +} diff --git a/template/components/title.html b/template/components/title.html new file mode 100644 index 0000000..eef5bf3 --- /dev/null +++ b/template/components/title.html @@ -0,0 +1,5 @@ +{{define "title"}} +
+

{{.Label}}

+
+{{end}} \ No newline at end of file diff --git a/templates.go b/templates.go new file mode 100644 index 0000000..38ef4f2 --- /dev/null +++ b/templates.go @@ -0,0 +1,36 @@ +// Copyright (c) 2024 Zeni Kim +// Use of this source code is governed by MIT-style +// license that can be found in the LICENSE file. + +package core + +import ( + "embed" + "html/template" + "io/fs" + "strings" +) + +var tmpl *template.Template = nil + +func NewTemplates(components embed.FS, templates embed.FS) { + // templates + var paths []string + fs.WalkDir(components, ".", func(path string, d fs.DirEntry, err error) error { + if strings.Contains(d.Name(), ".html") { + paths = append(paths, path) + } + return nil + }) + + var pathst []string + fs.WalkDir(templates, ".", func(patht string, d fs.DirEntry, err error) error { + if strings.Contains(d.Name(), ".html") { + pathst = append(pathst, patht) + } + return nil + }) + + tmpla := template.Must(template.ParseFS(components, paths...)) + tmpl = template.Must(tmpla.ParseFS(templates, pathst...)) +}