From 71facc227da893732316385a42ffae1577a44929 Mon Sep 17 00:00:00 2001 From: 0ceanSlim Date: Fri, 17 May 2024 15:53:56 -0400 Subject: [PATCH] better templating --- .gitignore copy | 2 ++ src/routes/example.go | 20 +++++++++++-- src/routes/root.go | 6 ++-- web/views/.layout.html | 16 +++++++++++ web/views/example.html | 14 ++------- web/views/index.html | 64 +++++++++++++++++------------------------- 6 files changed, 66 insertions(+), 56 deletions(-) create mode 100644 .gitignore copy create mode 100644 web/views/.layout.html diff --git a/.gitignore copy b/.gitignore copy new file mode 100644 index 0000000..05988f2 --- /dev/null +++ b/.gitignore copy @@ -0,0 +1,2 @@ +output.css +config.json \ No newline at end of file diff --git a/src/routes/example.go b/src/routes/example.go index 742b46f..c4b4987 100644 --- a/src/routes/example.go +++ b/src/routes/example.go @@ -1,7 +1,23 @@ package routes -import "net/http" +import ( + "html/template" + "net/http" +) func ExampleHandler(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, "web/views/example.html") + data := PageData{ + Title: "Example Page", + } + + tmpl, err := template.ParseFiles("web/views/.layout.html", "web/views/example.html") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + err = tmpl.ExecuteTemplate(w, "layout", data) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } } \ No newline at end of file diff --git a/src/routes/root.go b/src/routes/root.go index a6be1ea..e9cd9be 100644 --- a/src/routes/root.go +++ b/src/routes/root.go @@ -14,14 +14,14 @@ func RootHandler(w http.ResponseWriter, r *http.Request) { Title: "Home Page", } - tmpl, err := template.ParseFiles("web/views/index.html") + tmpl, err := template.ParseFiles("web/views/.layout.html", "web/views/index.html") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - err = tmpl.Execute(w, data) + err = tmpl.ExecuteTemplate(w, "layout", data) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } -} \ No newline at end of file +} diff --git a/web/views/.layout.html b/web/views/.layout.html new file mode 100644 index 0000000..8490629 --- /dev/null +++ b/web/views/.layout.html @@ -0,0 +1,16 @@ +{{define "layout"}} + + + + + + GO Web App - {{.Title}} + + + + + + {{template "view" .}} + + +{{end}} diff --git a/web/views/example.html b/web/views/example.html index c93768b..fb54cf1 100644 --- a/web/views/example.html +++ b/web/views/example.html @@ -1,13 +1,4 @@ - - - - - - Example Route - - - - +{{define "view"}}

This is an additional example route @@ -23,5 +14,4 @@

© 2024 My Web App

- - + {{end}} diff --git a/web/views/index.html b/web/views/index.html index ea54cf8..94e4e0d 100644 --- a/web/views/index.html +++ b/web/views/index.html @@ -1,41 +1,27 @@ - - - - - - GO Web App - - - - - -
-

- Welcome to My GO Web App Framework {{.Title}} -

-
+{{define "view"}} +
+

+ Welcome to My GO Web App Framework {{.Title}} +

+
-
-
- -

API Example

- Access Example API -

Content Section

-

- This is the main content of your web app. You can add any HTML content - here. -

- Another Example Route can be found here -
-
+
+
+ +

API Example

+ Access Example API +

Content Section

+

+ This is the main content of your web app. You can add any HTML content + here. +

+ Another Example Route can be found here +
+
-
-

© 2024 My Web App

-
- - + +{{end}}